Home Web Front-end CSS Tutorial Detailed explanation of the use of selectors nth-child and :nth-of-type

Detailed explanation of the use of selectors nth-child and :nth-of-type

Mar 21, 2018 am 11:17 AM
nth-child Selector

This time I will bring you a detailed explanation of the use of the selectors nth-child and :nth-of-type. What are the precautions when using the selectors nth-child and :nth-of-type? The following is a practical case, let’s take a look.

Let’s look at a simple example, first the HTML part:

<section>
    <p>我是第1个p标签</p>
    <p>我是第2个p标签</p>  <!-- 希望这个变红 -->
</section>
Copy after login

Then the CSS code corresponding to the two selectors is as follows:

p:nth-child(2) { color: red; }
Copy after login
Copy after login
p:nth-of-type(2) { color: red; }
Copy after login
Copy after login

In the above example, this The effects achieved by the two selectors are consistent. The text of the second p tag turns red:

Although the final effects of the above two demos are the same, Differences between the two selectors are inevitable.

For the :nth-child selector, in simple vernacular, it means selecting an element:

1. This is a paragraph element
2. This is The second child element of the parent tag

For the :nth-of-type selector, it means selecting an element:

1. Select the second child element of the parent tag Paragraph sub-element

If we slightly modify the above example, we can see the difference between the two selectors, as shown in the following HTML code:

<section>
    <p>我是一个普通的p标签</p>
    <p>我是第1个p标签</p>
    <p>我是第2个p标签</p>  <!-- 希望这个变红 -->
</section>
Copy after login

Still the same as the above example Consistent CSS test code:

p:nth-child(2) { color: red; }
Copy after login
Copy after login

p:nth-of-type(2) { color: red; }
Copy after login
Copy after login

##At this time, the results rendered by the two selectors are different.

p:nth-child(2) The rendering result is not that the text of the second p tag turns red, but the first p tag, which is the second child element of the parent tag.

The performance of p:nth-of-type(2) appears to be very strong, and it dyes the second p tag that you want to render red.

I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!

Recommended reading:

Detailed explanation of the use of pointer-events in css3

Detailed explanation of the use of focus-within

The above is the detailed content of Detailed explanation of the use of selectors nth-child and :nth-of-type. For more information, please follow other related articles on the PHP Chinese website!

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

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

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What is the identifier of the id selector in css What is the identifier of the id selector in css Sep 22, 2022 pm 03:57 PM

In CSS, the identifier of the id selector is "#". You can specify a specific style for the HTML element marked with a specific id attribute value. The syntax structure is "#ID value {attribute: attribute value;}". The ID attribute is unique and non-repeatable in the entire page; the ID attribute value should not start with a number. IDs starting with numbers will not work in Mozilla/Firefox browsers.

Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3 Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3 Nov 20, 2023 am 11:20 AM

Use the :nth-child(n+3) pseudo-class selector to select the style of child elements whose position is greater than or equal to 3. The specific code example is as follows: HTML code: &lt;divid="container"&gt;&lt;divclass="item"&gt ;First child element&lt;/div&gt;&lt;divclass="item"&

What to do if the javascript selector fails What to do if the javascript selector fails Feb 10, 2023 am 10:15 AM

The JavaScript selector fails because the code is not standardized. The solution is: 1. Remove the imported JS code and the ID selector method will be effective; 2. Just introduce the specified JS code before introducing "jquery.js".

css pseudo-selector learning pseudo-class selector analysis css pseudo-selector learning pseudo-class selector analysis Aug 03, 2022 am 11:26 AM

In the previous article "Css Pseudo-Selector Learning - Pseudo-Element Selector Analysis", we learned about pseudo-element selectors, and today we take a closer look at pseudo-class selectors. I hope it will be helpful to everyone!

Do selectors in css include hypertext tag selectors? Do selectors in css include hypertext tag selectors? Sep 01, 2022 pm 05:25 PM

Not included. CSS selectors include: 1. Tag selector, which locates specific HTML elements through the element name of the HTML page; 2. Class selector, which locates specific HTML elements through the value of the class attribute of the HTML element; 3. ID selector, which Locate specific HTML elements through the value of the id attribute of the HTML element; 4. The wildcard selector "*" can refer to all types of tag elements, including custom elements; 5. The attribute selector uses the existing attribute name of the HTML element or attribute value to locate a specific HTML element.

In-depth analysis of is and where selectors: improving CSS programming level In-depth analysis of is and where selectors: improving CSS programming level Sep 08, 2023 pm 08:22 PM

In-depth analysis of is and where selectors: improving the level of CSS programming Introduction: In the process of CSS programming, selectors are an essential element. They allow us to select and style elements in an HTML document based on specific criteria. In this article, we will take a deep dive into two commonly used selectors namely: is selector and where selector. By understanding their working principles and usage scenarios, we can greatly improve the level of CSS programming. 1. is selector is selector is a very powerful choice

From beginner to proficient: Master the skills of using is and where selectors From beginner to proficient: Master the skills of using is and where selectors Sep 08, 2023 am 09:15 AM

From beginner to proficient: Master the skills of using is and where selectors Introduction: In the process of data processing and analysis, the selector is a very important tool. Through selectors, we can extract the required data from the data set according to specific conditions. This article will introduce the usage skills of is and where selectors to help readers quickly master the powerful functions of these two selectors. 1. Use of the is selector The is selector is a basic selector that allows us to select the data set based on given conditions.

What is nth-child? What is nth-child? Aug 04, 2023 am 11:00 AM

, the `nth-child` selector provides developers with a more precise and flexible way to select and style elements. By properly using the `nth-child` selector, we can easily select and style various elements with specific positions, making our page design richer and more diverse.

See all articles