Home > Web Front-end > Front-end Q&A > What selector should be used to select the sub-element in css?

What selector should be used to select the sub-element in css?

青灯夜游
Release: 2023-01-06 11:14:50
Original
4254 people have browsed it

In CSS, if you want to select the child element of a specified element, you can use the ":nth-child()" selector. The ":nth-child()" selector can match the Nth child element belonging to its parent element, regardless of the type of element; and N can be a number, keyword, or formula.

What selector should be used to select the sub-element in css?

The operating environment of this tutorial: Windows 7 system, CSS3&&HTML5 version, Dell G3 computer.

css selector specifies the number of child elements in the element

:nth-child(n) The selector matches the nth child element that belongs to its parent element N child elements, regardless of element type.

n can be a number, keyword or formula.

Example:

tr td:nth-child(2){
    background-color:gray;
}
Copy after login

is the attribute of the second td of td in tr

tr:nth-child(2n+0){
    background-color:#F0F0F0;
}
Copy after login

This is a multiple of 2 of tr

Use the formula (an b). Description: Indicates the length of the period, n is the counter (starting from 0), and b is the offset value.

Here, we specify the background color of all p elements whose subscripts are multiples of 3:

p:nth-child(3n+0)
{
background:#ff0000;
}
Copy after login

(Learning video sharing: css video tutorial)

The above is the detailed content of What selector should be used to select the sub-element in css?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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