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 Selectors Advanced Selectors :nth-of-type

Suzanne Roys
Suzanne Roys
3,170 Points

Using classes/IDs vs. nth of type/nth child selectors, etc

Is it usually preferable to use classes/IDs, etc when we can, and stick to using nth of type/nth child selectors only when we have to or when we have no access to HTML? I guess what I'm asking is, are these selectors part of a standard design process or kept for more of a last resort scenario? Thanks!

2 Answers

Dale Severude
seal-mask
.a{fill-rule:evenodd;}techdegree seal-36
Dale Severude
Full Stack JavaScript Techdegree Graduate 71,350 Points

They should be part of the standard design process and can be used to easily select specific child elements that otherwise would be very cumbersome to code. In cases of dynamic content they may be the only solution.

Diana Le
Diana Le
6,794 Points

For me they're really useful when it would be too inefficient to add classes to the html. For instance if I had a long <table> element and I wanted to alternate the colors of the rows, it would be much easier to use even or odd nth-child CSS rather than add a class to every other <tr> in the html.