Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Apply style to parent if it has child with CSS [duplicate]

Tags:

html

css

People also ask

Can CSS be used to find child => parent page element?

There is currently no way to select the parent of an element in CSS, at least not a way that works across all browsers.

Is there a CSS parent selector?

Let's be clear here, just in case someone is finding this from a search engine: there are no parent selectors in CSS, not even in CSS3.


It's not possible with CSS3. There is a proposed CSS4 selector, $, to do just that, which could look like this (Selecting the li element):

ul $li ul.sub { ... }

See the list of CSS4 Selectors here.

As an alternative, with jQuery, a one-liner you could make use of would be this:

$('ul li:has(ul.sub)').addClass('has_sub');

You could then go ahead and style the li.has_sub in your CSS.