The difference between pseudo-class Nth-child and Nth-of-type in CSS3

Source: Internet
Author: User

This post consists of: http://xinpure.com/css3-pseudo-class-difference-between-nthchild-and-nthoftype/

First look at the common denominator of Nth-child (n) and Nth-of-type (n)

The only common denominator is: the use of parameter n

n can be a number, a keyword, or a formula (n > 0)

Nth-child (2) represents the second child element of its parent element

Nth-child (2n) or nth-child (even) indicates the number of child elements of its parent element

Nth-child (2n-1) or nth-child (odd) represents the odd number of child elements of its parent element

And so on. Other formulas about N

Difference Analysis about Nth-child
p:nth-child(2) {    background: #ccc}

The second child of the parent element that acts on p, which is invalid if the element is not p

About Nth-of-type
p:nth-of-type(2) {    background: red}

The second P element (child element) that acts on the parent element of P

Difference Example HTML Code:
CSS Code
p:nth-child(2) {    background: #ccc}p:nth-of-type(2) {    background: #red;}
Result diagram

The difference between pseudo-class Nth-child and Nth-of-type in CSS3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.