Home  >  Article  >  Web Front-end  >  The difference between nth-child and :nth-of-type

The difference between nth-child and :nth-of-type

WBOY
WBOYOriginal
2016-08-26 10:13:171360browse

Why is:nth-of-type called:nth-of-type? Because it is distinguished by "type". That is to say: ele:nth-of-type(n) refers to the nth ele element under the parent element,

and ele:nth-child(n) refers to the nth element under the parent element and this element is ele , if not, the selection fails.

Example:

 

1

  

1

  

2

.box p:nth-child(1){

 Color:red; //Selection failed because the first child element under .box is not p

}

.box p:nth-child(2){

 Color:red; //The selected element is

1

}

.box p:nth-child(3){

 color:red; //The selected element is

2

}

.box p:nth-of-type(1){

 color:red; //The selected element is

1

}

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