search
HomeWeb Front-endCSS TutorialCSS descendant selector

This time I will bring you the CSS descendant selector. What are the precautions when using the CSS descendant selector? Here is a practical case, let’s take a look.

The descendant selector is used to select all descendants of a tag, including children and grandchildren, etc.; while the descendant selector only selects the descendant tags of the specified parent (the first generation child elements of the specified tag element) .

The descendant selector uses an extra symbol (angle brackets > ) to indicate the relationship between two elements.

For example: body>h1 selects all the first-generation

tags in the tag.

Relationship diagram of HTML tag

##HTML code

<h1 id="body里面的h-标题">body里面的h1标题</h1>
<p>
    </p><h2 id="p里面的h">p里面的h2</h2>
    <p>
        p里面的p标签,p标签里面有一个<strong><a>加粗的链接</a></strong>
    </p>

<h2 id="body里面的h-标题">body里面的h2标题</h2>
        
  • 列表1         
                  
    • 小列表a
    •             
    • 小列表b
    •             
    • 小列表c
    •         
        
  •     
  •         列表2(带链接)     
  •     
  •         列表3(带链接)     
After understanding the above structure, you can try the following code:

CSS code

body>h2 {
    color: orange;
}

In the above HTML code, there are a total of 2 < ;h2> tag, but has only one child of

, and the other

is inside

, so the above CSS code only works on the first

Label.

The following is a more interesting child selector

:first-child

Select the first sub-tag.

CSS code

h2:first-child {
    color: orange;
}

The function of this selector is: first find all

tags in the web page, and then find them through the

tag parent element, and then determine whether the

tag is ranked first in its parent element.

Here because the first tag of is

, the child element

of is not affected by the style.

Because

in

is the first child element of

, so the

in

becomes orange.

:last-child

This selector is similar to the :first-child selector, but it selects the last child of an element.

CSS code

li:last-child {
    font-size: 2em;
}

You can see that the font size of "Small List C" and "List 3 (with link)" has become larger. Because these two items are the last items specified.

:only-child

Select the only child of an element.

HTML code

<p>
    </p><p>第一个p的p</p>

<p>
    </p><p>第二个p的第一个p</p>
    <a>第二个p的第一个a</a>
CSS code

p:only-child {
    color: orange;
}

The above styles only apply to the first

element. Since there are not only

elements in the second

, but also an element, the

element of the second

will not be affected by the above style.

This selector is difficult to understand. The style defined by this selector is only effective if the specified tag is the only descendant of another tag. In other words, it is not enough if there is only one specified tag in the descendant. If the specified tag has other sibling tags, the style defined by this selector will be invalid.

:nth-child

This selector is more complicated to use, but it is also particularly useful.

This selector can easily define styles for rows in a table, items two items apart in a list, or other numbers of descendant elements.

This selector requires a value to determine which descendants are selected.

The simplest values ​​are keywords, namely odd and even.

  1. odd用于选取奇数子代元素。

  2. even用于选取偶数子代元素。

HTML代码


        
  • 1
  •     
  • 2
  •     
  • 3
  •     
  • 4
  •     
  • 5
  •     
  • 6
  •     
  • 7

CSS代码

li:nth-child(odd) {
    background: pink;
}
li:nth-child(even) {
    background: teal;
}

第一个子元素的下标是 1。

需要注意的是,如果在第一个

  • 之前还有其他元素,那第一个
  • 的下标就不是1了。
    比如
  • HTML代码

    
    
          a     
    • 1
    •     
    • 2
    •     
    • 3
    •     
    • 4
    •     
    • 5
    •     
    • 6
    •     
    • 7

    这时的效果是

    li:nth-child(odd) 的意思是找到

  • 元素的父元素,通过父元素来检测每一个
  • 的下标的奇偶。

    因为

  • The above is the detailed content of CSS descendant selector. For more information, please follow other related articles on the PHP Chinese website!

    Statement
    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
    What does margin: 40px 100px 120px 80px signify?What does margin: 40px 100px 120px 80px signify?Apr 28, 2025 pm 05:31 PM

    Article discusses CSS margin property, specifically "margin: 40px 100px 120px 80px", its application, and effects on webpage layout.

    What are the different CSS border properties?What are the different CSS border properties?Apr 28, 2025 pm 05:30 PM

    The article discusses CSS border properties, focusing on customization, best practices, and responsiveness. Main argument: border-radius is most effective for responsive designs.

    What are CSS backgrounds, list the properties?What are CSS backgrounds, list the properties?Apr 28, 2025 pm 05:29 PM

    The article discusses CSS background properties, their uses in enhancing website design, and common mistakes to avoid. Key focus is on responsive design using background-size.

    What are CSS HSL Colors?What are CSS HSL Colors?Apr 28, 2025 pm 05:28 PM

    Article discusses CSS HSL colors, their use in web design, and advantages over RGB. Main focus is on enhancing design and accessibility through intuitive color manipulation.

    How can we add comments in CSS?How can we add comments in CSS?Apr 28, 2025 pm 05:27 PM

    The article discusses the use of comments in CSS, detailing single-line and multi-line comment syntaxes. It argues that comments enhance code readability, maintainability, and collaboration, but may impact website performance if not managed properly.

    What are CSS Selectors?What are CSS Selectors?Apr 28, 2025 pm 05:26 PM

    The article discusses CSS Selectors, their types, and usage for styling HTML elements. It compares ID and class selectors and addresses performance issues with complex selectors.

    Which type of CSS holds the highest priority?Which type of CSS holds the highest priority?Apr 28, 2025 pm 05:25 PM

    The article discusses CSS priority, focusing on inline styles having the highest specificity. It explains specificity levels, overriding methods, and debugging tools for managing CSS conflicts.

    In how many ways can we add CSS to our HTML file?In how many ways can we add CSS to our HTML file?Apr 28, 2025 pm 05:24 PM

    Article discusses three methods to add CSS to HTML: inline, internal, and external. Each method's impact on website performance and suitability for beginners is analyzed.(159 characters)

    See all articles

    Hot AI Tools

    Undresser.AI Undress

    Undresser.AI Undress

    AI-powered app for creating realistic nude photos

    AI Clothes Remover

    AI Clothes Remover

    Online AI tool for removing clothes from photos.

    Undress AI Tool

    Undress AI Tool

    Undress images for free

    Clothoff.io

    Clothoff.io

    AI clothes remover

    Video Face Swap

    Video Face Swap

    Swap faces in any video effortlessly with our completely free AI face swap tool!

    Hot Tools

    Dreamweaver CS6

    Dreamweaver CS6

    Visual web development tools

    WebStorm Mac version

    WebStorm Mac version

    Useful JavaScript development tools

    Atom editor mac version download

    Atom editor mac version download

    The most popular open source editor

    VSCode Windows 64-bit Download

    VSCode Windows 64-bit Download

    A free and powerful IDE editor launched by Microsoft

    DVWA

    DVWA

    Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software