Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

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