Is it possible for me to style every 3rd list item?
Currently in my 960px wide div I have list of boxes which are floated left and are displayed in a 3×3 grid view. They also have a margin-right of 30px, but because the 3rd 6th and 9th list item has this margin it makes them jump down a row making the grid display wrongly
How easy is to make the 3rd 6th and 9th not have the margin-right without having to give them a different class, or is that the only way to do it?
Yes, you can use what’s known as
:nth-childselectors.In this case you would use:
:nth-child(3n):
:nth-child()is compatible in Chrome, Firefox, and IE9+.For a work around to use
:nth-child()amongst other pseudo-classes/attribute selectors in IE6 through to IE8, see this link.