Home Web Front-end CSS Tutorial Detailed explanation of css pseudo-class nth-child() example

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

Nov 07, 2018 am 10:31 AM
css pseudo-class

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; }
Copy after login

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; 
}
...
Copy after login

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);
}
Copy after login

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;
}
Copy after login

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 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

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

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)

Hot Topics

Java Tutorial
1658
14
PHP Tutorial
1257
29
C# Tutorial
1231
24
Google Fonts   Variable Fonts Google Fonts Variable Fonts Apr 09, 2025 am 10:42 AM

I see Google Fonts rolled out a new design (Tweet). Compared to the last big redesign, this feels much more iterative. I can barely tell the difference

How to Create an Animated Countdown Timer With HTML, CSS and JavaScript How to Create an Animated Countdown Timer With HTML, CSS and JavaScript Apr 11, 2025 am 11:29 AM

Have you ever needed a countdown timer on a project? For something like that, it might be natural to reach for a plugin, but it’s actually a lot more

HTML Data Attributes Guide HTML Data Attributes Guide Apr 11, 2025 am 11:50 AM

Everything you ever wanted to know about data attributes in HTML, CSS, and JavaScript.

A Proof of Concept for Making Sass Faster A Proof of Concept for Making Sass Faster Apr 16, 2025 am 10:38 AM

At the start of a new project, Sass compilation happens in the blink of an eye. This feels great, especially when it’s paired with Browsersync, which reloads

How We Created a Static Site That Generates Tartan Patterns in SVG How We Created a Static Site That Generates Tartan Patterns in SVG Apr 09, 2025 am 11:29 AM

Tartan is a patterned cloth that’s typically associated with Scotland, particularly their fashionable kilts. On tartanify.com, we gathered over 5,000 tartan

How to Build Vue Components in a WordPress Theme How to Build Vue Components in a WordPress Theme Apr 11, 2025 am 11:03 AM

The inline-template directive allows us to build rich Vue components as a progressive enhancement over existing WordPress markup.

PHP is A-OK for Templating PHP is A-OK for Templating Apr 11, 2025 am 11:04 AM

PHP templating often gets a bad rap for facilitating subpar code — but that doesn't have to be the case. Let’s look at how PHP projects can enforce a basic

Programming Sass to Create Accessible Color Combinations Programming Sass to Create Accessible Color Combinations Apr 09, 2025 am 11:30 AM

We are always looking to make the web more accessible. Color contrast is just math, so Sass can help cover edge cases that designers might have missed.

See all articles