You can use :has() pseudo-class (status: working draft, specifications, MDN, caniuse) for this:
li:has(> a.active) {
outline: thin dotted red;
}
<ul>
<li><a href="#">a</a></li>
<li><a href="#" class="active">a.active</a></li>
<li><a href="#">a</a></li>
</ul>
In fact, this pseudo-class can be used to match an element that has a specific element as child, grand child, next sibling or subsequent sibling:
section, h1, p {
font-size: 1rem;
margin-top: 1rem;
margin-bottom: 1rem;
}
h1:has(> .ref) {
outline: thin dotted red;
}
h1:has(.ref) {
outline: thin dotted red;
}
h1:has(+ .ref) {
outline: thin dotted red;
}
h1:has(~ .ref) {
outline: thin dotted red;
}
<section>
<h1>
heading
<span class="ref">.ref</span>
</h1>
</section>
<section>
<h1>
heading
<span>
span
<span class="ref">.ref</span>
</span>
</h1>
</section>
<section>
<h1>heading</h1>
<p class="ref">.ref</p>
</section>
<section>
<h1>heading</h1>
<p>paragraph</p>
<p class="ref">.ref</p>
</section>
The concerns I raised in my original answer are still relevant:
For example, if you wrote:
body:has(a.active) { background: red; }Then the browser will have to wait until it has loaded and parsed everything until the to
</body>to determine if the page should be red or notor not.