search
HomeWeb Front-endCSS TutorialDetailed explanation of css pseudo-class nth-child() example

This article brings you a detailed explanation of the css pseudo-class nth-child() example, so that everyone can understand what the nth-child() pseudo-class can do. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

The nth-child() pseudo-class in CSS3 is very useful for creating formatted Excel stylesheets in HTML. Also used to generate grid layouts without having to resort to tables.

First of all, let’s learn about the nth-child() pseudo-class. [Recommended related video tutorials: css tutorial, css3 tutorial! 】

The basic rules of nth-child() pseudo-class:

The syntax we use is: nth-child (a n b) where a is the frequency and b is the initial offset. This generates an infinite series starting at n = 0, but containing only positive values.

Some examples might make this clearer:

2n, 2n 0
2,4,6,8,10,12......

2n 1 or odd number
1,3,5,7,9,11......

2n 2
2,4,6,8,10,12......

2n 3
3,5,7,9,11,12......

2n 4
4,6,8,10,12,14......

3n, 3n 0 or 3n 3
3,6,9,12,15,18......

3n 1
​ 1,4,7,10,13,16…

So you can see that the series starts with b and then increases by a for each value. Skipping any zero or negative results means we can't look backwards in the DOM tree.

Example of nth-child() pseudo-class:

Next let’s take a look at the nth-child() pseudo-class through examples Function:

Example 1: Used with hover

This example uses the nth-child pseudo-class and ~ general sibling selector.

First we create the grid by simply floating multiple div containers to the left and using nth-child to start a new row after every ten boxes:

#stage div {
    float: left;
    margin: 5px;
    width: 60px;
    height: 50px;
    background: #efefef;
}
#stage div:hover { background: red; }
#stage div:nth-child(10n+1) { clear: left; }

In HTML , we added an id to each div container (#div1, #div2, ..., #div100) and then assigned a hover event like this:

#div1:hover ~ div:nth-child(1n) {
   background: yellow; 
}
#div2:hover ~ div:nth-child(2n) {
   background: yellow; 
}
#div3:hover ~ div:nth-child(3n) {
   background: yellow; 
}
#div4:hover ~ div:nth-child(4n) {
   background: yellow; 
}
...

This means when the cursor When on a div, every nth sibling of this div will turn yellow. For example, when the mouse hovers over the number 3 (#div3), it turns red, and every div that is a multiple of 3 turns yellow. Try it, let’s see the effect:

Detailed explanation of css pseudo-class nth-child() example

Example 2: Use nth-child to format the table

In CSS A more typical example is how to format HTML tables to make them look more professional. For example: Alternating column or row colors:

Detailed explanation of css pseudo-class nth-child() example

While not very pretty, the markup is very simple and you can easily change the colors. For the 'tartan' table effect, we use a background color that has some alpha transparency, so when the column (red) and row (blue) colors meet, a third (purple) color is produced.

For this example, the class value of the table table is "tartan":

.tartan tr:nth-child(odd) {
    background: rgba(0,0,255,0.5);
}
.tartan td:nth-child(even) {
    background: rgba(255,0,0,0.5);
}

If we want to directly position the intersecting cells so that we can specify other colors without a transparent background, we can also Usage:

.tartan tr:nth-child(odd) td:nth-child(even) {
    background: #fff;
}

This is for the odd cells in all odd rows and the even cells in even rows of the table. Let’s see the effect:

Detailed explanation of css pseudo-class nth-child() example

In the above styles, you should notice that we use the abbreviation method of odd and even numbers, which is easier to remember.

Summary: The above is the entire introduction to the use of the nth-child() pseudo-class in this article. You can use the nth-child() pseudo-class yourself to achieve effects and deepen your understanding. I hope it can help everyone learn Helps.

The above is the detailed content of Detailed explanation of css pseudo-class nth-child() example. 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 is CSS Grid?What is CSS Grid?Apr 30, 2025 pm 03:21 PM

CSS Grid is a powerful tool for creating complex, responsive web layouts. It simplifies design, improves accessibility, and offers more control than older methods.

What is CSS flexbox?What is CSS flexbox?Apr 30, 2025 pm 03:20 PM

Article discusses CSS Flexbox, a layout method for efficient alignment and distribution of space in responsive designs. It explains Flexbox usage, compares it with CSS Grid, and details browser support.

How can we make our website responsive using CSS?How can we make our website responsive using CSS?Apr 30, 2025 pm 03:19 PM

The article discusses techniques for creating responsive websites using CSS, including viewport meta tags, flexible grids, fluid media, media queries, and relative units. It also covers using CSS Grid and Flexbox together and recommends CSS framework

What does the CSS box-sizing property do?What does the CSS box-sizing property do?Apr 30, 2025 pm 03:18 PM

The article discusses the CSS box-sizing property, which controls how element dimensions are calculated. It explains values like content-box, border-box, and padding-box, and their impact on layout design and form alignment.

How can we animate using CSS?How can we animate using CSS?Apr 30, 2025 pm 03:17 PM

Article discusses creating animations using CSS, key properties, and combining with JavaScript. Main issue is browser compatibility.

Can we add 3D transformations to our project using CSS?Can we add 3D transformations to our project using CSS?Apr 30, 2025 pm 03:16 PM

Article discusses using CSS for 3D transformations, key properties, browser compatibility, and performance considerations for web projects.(Character count: 159)

How can we add gradients in CSS?How can we add gradients in CSS?Apr 30, 2025 pm 03:15 PM

The article discusses using CSS gradients (linear, radial, repeating) to enhance website visuals, adding depth, focus, and modern aesthetics.

What are pseudo-elements in CSS?What are pseudo-elements in CSS?Apr 30, 2025 pm 03:14 PM

Article discusses pseudo-elements in CSS, their use in enhancing HTML styling, and differences from pseudo-classes. Provides practical examples.

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

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.