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.

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