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


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.

Updated on: 25-Jun-2020

474 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements