Home >Web Front-end >CSS Tutorial >If the parent has a child with CSS and HTML, apply styles to the parent
Parent selector does not exist in CSS3. For this, it is recommended to use the CSS4 selector $, which might look like this (selecting the li element) -
ul $li ul.sub { ... }
As an alternative, using jQuery, you can achieve this functionality with a single line of code. The :has() selector selects all elements that contain one or more elements that match the specified selector. 25edfb22a4f469ecb59f1190150159c6The tag defines the list item. ff6d136ddc5fdfeffaf53ff6ee95f185 Tags define an unordered (bulleted) list.
$('ul li:has(ul.sub)').addClass('has_sub');
You can add styles to li.has_sub in CSS.
The above is the detailed content of If the parent has a child with CSS and HTML, apply styles to the parent. For more information, please follow other related articles on the PHP Chinese website!