Is there a CSS parent selector?
P粉176980522
2023-08-23 10:53:50
<p>How do I select a <code></code></p><li> element that is the direct parent of an anchor element? <p><br /></p>
<p>For example, my CSS would look like this: </p>
<pre class="brush:php;toolbar:false;">li < a.active {
property: value;
}</pre>
<p>Obviously there are ways to do this using JavaScript, but I'm hoping there's some kind of workaround for CSS Level 2 itself. </p>
<p>The menu I'm trying to style is spewed out by the CMS, so I can't move the active element to the <code></code></p></li><li> element. .. (unless I theme the menu and create a module, which I don't want to do). <p><br /></p></li>
You can use
:has()
CSS pseudo-classBut it has limited browser support (currently not supported by Firefox).
There is currently no way to select the parent element of an element in CSS in a way that works in all browsers.
Selector Level 4 Working Draft Includes
:has()
Pseudo class that will provide this functionality. It would be similar to the jQuery implementation, but is currently not supported by Firefox.Firefox is currently the only major browser that does not support it by default.
Meanwhile, if you need to select a parent element with full cross-browser support, you'll have to resort to JavaScript in Firefox.