About the pen

What's this?

This pen was quickly created to demonstrate how :has() and :target can potentially be used in tandem to make changes to various elements that either surround the targeted element or an element that is an ascendant to the :target. This also doubles as a proposal for :target-within , which could behave as a target counterpart to :focus-within .

It is part of a series of pens in which I try to showcase the potential usefulness of :has() , which is currently supported by zero browsers as of this pen's publishing .

How it works

Click on any link within the navbar to get a feel for how :has() and :target can be used to give links within it an "active"/highlighted appearance as well as make changes to elements that happen to contain a target.

About the CSS

CSS Code what-if

Much like the other pens in this series, this section contains a bit of CSS code that provides a rough idea as to how the CSS code could look if :has() were to be supported.

	#nav {
		background: #455A64;
		/* [...] */
	}
	#nav a {
		/* [...] */
	}
	#nav a:hover {
		background-color: #546E7A;
		/* [...] */
	}
	/* Highlight #nav link when an element is :targeted */
	body:has(#s1:target) > #header #nav a[href="#s1"],
	body:has(#s2:target) > #header #nav a[href="#s2"],
	body:has(#s3:target) > #header #nav a[href="#s3"],
	body:has(#s4:target) > #header #nav a[href="#s4"] {
		background-color: #5D4037;
	}
	body:has(#s1:target) > #header #nav a[href="#s1"]:hover,
	body:has(#s2:target) > #header #nav a[href="#s2"]:hover,
	body:has(#s3:target) > #header #nav a[href="#s3"]:hover,
	body:has(#s4:target) > #header #nav a[href="#s4"]:hover {
		background-color: #6D4C41;
	}

	.section {
		background: #fff;
		/* [...] */
	}
	.section .subsection {
		background: #fff;
		/* [...] */
	}
	.section:has(:target) {
		background: #AED581;
	}
	.section .subsection:target {
		background: #FFF9C4;
	}