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

How would I efficiently select groups between the first and last elements? (:nth-of-type)

Hey! I was wondering the best method of selecting a group of adjacent elements between the first and last elements. For example: I have ten elements, but I only want to select the second through fourth elements. Is there a way to use the nth-of-type selector for that? If not, what is the best way?

3 Answers

You want: li:nth-child(n+4):nth-child(-n+8)

source: http://nthmaster.com/

demo on codepen

Wow, I've never heard of that! Thanks a lot!

Genzo Mallowigi - You can test out your nth-child calculations here: http://codepen.io/jamesbarnett/pen/Fanbj

Thanks!

nth-of-type(2), nth-of-type(3), nth-of-type(4)

Yes, there's no other ways (AFAIK)

> Yes, there's no other ways (AFAIK)

You have to combine 2 nth-child selectors

I'm sure you can do this using :nth-child(-2n+4) i think its covered in advanced selectors section of CSS.

(I'm sure someone will correct me if I'm wrong, but I'm pretty sure I'm on the right track)

That will only select the 2nd and 4th elements.