The forums ran from 2008-2020 and are now closed and viewable here as an archive.

Home Forums CSS [Solved] Targeting – Advanced Selector Reply To: [Solved] Targeting – Advanced Selector


If you always want to target the first element with class section-box, AND the last element with class section-box, AND you know that sometimes there may be elements before or after that, you’ll probably have to use some JavaScript.

first-child and last-child won’t do, since they won’t always be the first or last children as you say.
nth-of-type won’t do, since there may be other divs (or whatever type you’re using) before or after it.

There are no other “pure CSS” solutions like these yet.

What you MAY want to check into, is clever use of margins and paddings. Let’s say you want a space of 5 pixels between each section box. You’ll also want a space of 20 pixels at the top of the first one, and a space of 40 pixels at the bottom of the last one.

You could accomplish that by:
– giving each section box a top margin of 0 and bottom margin of 5 pixels
– give the container a top padding of 20 pixels
– give the container a bottom padding of 35 pixels

Would that be an option?