CSS3 selector: The difference between Nth-child and Nth-of-type

Source: Internet
Author: User

<section>

<p> I am the 1th p tag </p>

<p> I am the 2nd p tag </p>

</section>

Then the corresponding CSS code for the 2 selectors is as follows:

P:nth_child (2) {color:red;}

P:nth-of-type (2) {color:red;}

The two corresponding effects are the same:

For: The Nth-child selector has two points to explain:

1, this is a paragraph element.

2, this is the second child of a parent element

For: The nth-of-type selector means selecting an element if:

1, select the second paragraph Child element of the parent element

If you change the above code a little bit:

<section>

<div> I am an ordinary div tag </div>

<p> I was the first P tag </p>

<p> I am the second P tag </p>

</section>

At this time, P:nth-child (2) {color:red;} did not select any of the elements, so it did not change.

P:nth-of-type (2) {color:red;} Displays the result:

CSS3 selector: The difference between Nth-child and Nth-of-type

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.