Apply style to the parent if it has a child with CSS and HTML

CSSWeb DevelopmentFront End Technology

Parent selectors are not present in CSS3. There is a proposed CSS4 selector, $, to do so, which could look like this (Selecting the li element) −

ul $li ul.sub { ... }

As an alternative, with jQuery, a one-liner you could make use of would be this. The: has() selector selects all elements that have one or more elements inside of them, that matches the specified selector. The <li> tag defines a list item. The <ul> tag defines an unordered (bulleted) list.

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

You could then style the li.has_sub in your CSS.

Published on 21-Mar-2018 14:26:51