Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

CSS CSS Foundations Advanced Selectors Pseudo-Classes: :nth-of-type and :only-of-type

Question about p:nth-child(odd)

Hello everyone,

My question is about the difference between

p:nth-child(odd)

and

p:nth-of-type(odd)

The CSS code below:

p:nth-child(odd)    {

    background-color: steelblue;


}

changes the background-color to steelblue for the 2nd paragraph, 4th paragraph, and 6th paragraph under Box1. I was expecting it to do 1, 3, and 5. Why is that?

But when I use

p:nth-of-type(odd)  {

    background-color: steelblue;


}

It changes p1, p2, and p3 which is what I expected.

HTML:

<body>
    <div>Box 1</div>
        <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodnth-of-type
        tempor incididunt ut labore et dolore magna aliqua.
    </p>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodnth-of-type
        tempor incididunt ut labore et dolore magna aliqua.
    </p>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodnth-of-type
        tempor incididunt ut labore et dolore magna aliqua.
    </p>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodnth-of-type
        tempor incididunt ut labore et dolore magna aliqua.
    </p>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodnth-of-type
        tempor incididunt ut labore et dolore magna aliqua.
    </p>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodnth-of-type
        tempor incididunt ut labore et dolore magna aliqua.
    </p>
    <div>Box 2</div>
        <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodnth-of-type
        tempor incididunt ut labore et dolore magna aliqua.
    </p>
    <div>Box 3</div>
        <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodnth-of-type
        tempor incididunt ut labore et dolore magna aliqua.
    </p>
    <div>Box 4</div>
    <p>
        Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmodnth-of-type
        tempor incididunt ut labore et dolore magna aliqua.
    </p>
    <p>
        Praesent vestibulum purus justo, vel viverra nulla porttitor ac. Curabitur laoreet 
        luctus erat, eu porta dolor lacinia ac. Nullam fermentum tellus nec neque gravida adipiscing at quis dui.
    </p>
</body>

Thank you for your help :)

1 Answer