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 trialThomas Hewitt
17,691 PointsHow do I select the last element of a group in CSS?
Hi everyone,
I'm trying to create a carousel of images using a background image sprite that I've made in CSS.
I have the image nested within a couple of elements. Starting with a div, then an <a> element within that div and then finally within the <a> there is another div which houses the individual background image sprites.
Now I know I could do this with a class but I want to learn how to use different selectors to improve my CSS.
How would I go about getting the last sprite to be a different width to the others using css?
Would I use some kind of nth of selector?
Right now all of them are 99px in with but I want the last one to be 80px. I could do it with inline styling or I could use a class but that's just avoiding learning how to use different types of selectors.
I appreciate the help from everyone as always.
1 Answer
Sean T. Unwin
28,690 PointsYou could use :last-child
e.g.
a > div > img:last-child { width: 80px; }
You may still want to use classes at least as identifiers for your containers or the <a>
tags so you can access the children in a reasonable fashion without lots of chaining in the declaration as in the example above.