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

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

青灯夜游
青灯夜游Original
2021-05-21 11:19:054152browse

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

is the attribute of the second td of td in tr

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

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

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

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