Home  >  Article  >  Web Front-end  >  If the parent has a child with CSS and HTML, apply styles to the parent

If the parent has a child with CSS and HTML, apply styles to the parent

WBOY
WBOYforward
2023-09-21 16:45:051155browse

如果父级有一个具有 CSS 和 HTML 的子级,则将样式应用于父级

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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete