search

Home  >  Q&A  >  body text

Is there a CSS parent selector?

<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>
P粉176980522P粉176980522453 days ago500

reply all(2)I'll reply

  • P粉704066087

    P粉7040660872023-08-24 10:03:55

    You can use :has() CSS pseudo-class

    But it has limited browser support (currently not supported by Firefox).

    reply
    0
  • P粉011360903

    P粉0113609032023-08-24 09:39:22

    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.

    li:has(> a.active) { /* styles to apply to the li tag */ }
    

    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.

    reply
    0
  • Cancelreply