Home > Web Front-end > CSS Tutorial > 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
Release: 2023-09-21 16:45:05
forward
1214 people have browsed it

如果父级有一个具有 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 { ... }
Copy after login

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.

  • The tag defines the list item.
      Tags define an unordered (bulleted) list.

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

      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!

  • source:tutorialspoint.com
    Statement of this Website
    The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
    Popular Tutorials
    More>
    Latest Downloads
    More>
    Web Effects
    Website Source Code
    Website Materials
    Front End Template