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

Source: Internet
Author: User

Instance:

First create an HTML structure

class="post">    <p> I am the first paragraph of the article </p>    <p> I am the second paragraph in the article </p></div>

Next use:. Nth-child and Nth-of-type to select and change their text color

. Post>p:nth-child (2) {            color: red;        }

. Post>p:nth-of-type (2) {            color: red;        }

Now add a Heading 1 to the front of the paragraph.

<Divclass= "POST">    <H1>I'm Heading 1.</H1>    <P>I am the first paragraph of the article</P>    <P>I am the second paragraph of the article</P></Div>

Re-use:. Nth-child and Nth-of-type Select and change the text color

. Post>p:nth-child (2) {            color:red;        }

. Post>p:nth-of-type (2) {            color: red;
}

Nth-child contains two meanings, first a paragraph element, the second part of the parent element "DIV" of the paragraph element, and "Nth-of-type" means to select the parent element of paragraph 2

If you add a H2 caption after the "H1" heading, "P:nth-child (2)" will not be able to select any elements at this point because the second element of DIV is not paragraph one "P" so you cannot select any elements. But "P:nth-of-type (2)" still works because the second paragraph "P" in Div is always selected.

Remember that "Nth-child" chooses the child element of the parent element, which does not specify the exact type, while satisfying two conditions: one is a child element, The second is exactly where the child element is located (that is, all child elements are positioned in nth-child parentheses, and the type is nth-child before the element type takes effect, otherwise it does not take effect); "Nth-of-type" selects a child element of a parent element. And this element is the specified type (that is, the child element type is the node of the specified type in parentheses within the specified number)

Really around the mouth, or look at the example of it ....

The difference between 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.