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

Android

How does nth-of-type know what parent?

so, basically, i get nth-of-type, but i don't see where a parent element is designated or even comes into this (except in the sense that i can't make a div in html without it being inside a parent element anyway)

As near as i can figure div:nth-of-type(2) for example, is just selecting the second div element out of all divs, because the "type" that the nth is targeting in this case is "div". I don't see where it is specifically targeting divs that are children of the body...

i guess what I'm really asking is how does it know which parent element this is a child of if you don't actually say that? or do we only use this if we are not wanting to exclude any of that type from the selection? (or can we additionally use it more specifically such as something like this: p.explanation div:nth-of-type??)

yup, very confused. maybe I'm just too tired... what am i not getting here?

1 Answer

James Barnett
James Barnett
39,199 Points

That's not nth-of-type does, it works similar to how nth-child does.

I used this these resources when I was first learning about nth-child