A common problem faced by many web developers is how to correctly handle a series of floated, margined elements inside a fixed width container. In case that doesn’t make sense, take a look at the grid of Flickr images on our homepage. Here we have a series of list items floated left inside an unordered list. The usual way of handling these might be to float all the list items left, add a margin to the bottom and right, and then add a class of something like .end to the last item in each row, to remove the right margin and preventing it from breaking.
The markup and CSS might be something like:
<ul id="images">
<li><a href="#"><img src="/img/001.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/002.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/003.jpg" alt="" /></a></li>
<li class="end"><a href="#"><img src="/img/004.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/005.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/006.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/007.jpg" alt="" /></a></li>
<li class="end"><a href="#"><img src="/img/008.jpg" alt="" /></a></li>
</ul>
#images{
width:460px;
overflow:hidden; /* This coupled with a width clears the floats -- as per http://csswizardry.com/floats/ */
}
#images li{
width:100px;
height:100px;
float:left;
margin:0 20px 20px 0; /* Note: margin applied bottom and right */
}
#images .end{
margin:0 0 20px 0;
}
This would give you a box-model overview like this:

This works perfectly well, the margin-right is removed and everything stacks up and lines up perfectly. However…
If you populate this list dynamically, like we do with our Flickr images, you need a script to work out the position of the end item (in this case 4th) and write out the .end class. Whilst not impossible, it’s certainly more work than is necessary. Also, if you ever decide you want to have 6 items per row, you would need to alter your script accordingly to add a class to the 6th item instead.
Furthermore, the .end class is both ugly and insemantic (and, after using the method I’m about to outline, totally extraneous).
The code
<ul id="images">
<li><a href="#"><img src="/img/001.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/002.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/003.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/004.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/005.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/006.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/007.jpg" alt="" /></a></li>
<li><a href="#"><img src="/img/008.jpg" alt="" /></a></li>
</ul>
#images{
width:480px; /* Width of element plus one times the width of the margins (460 + 20 = 480) */
margin-left:-20px; /* Width of one times the margins */
overflow:hidden; /* This coupled with a width clears the floats -- as per http://csswizardry.com/floats/ */
}
#images li{
width:100px;
height:100px;
float:left;
margin:0 0 20px 20px; /* Note: margin now applied bottom and left */
}
N.B. Substitute every instance of 20px here with whatever your margin size is.
This works far nicer. What we do here is put all the margins on the left, whch pushes everything over to the right by 20 pixels too much. It also makes the ul itself 20px wider than it needs to be. To undo the effects of this we set the width of the ul to 20px more than we want, then pull the entire ul back over to the left by the same 20px.
The box-model overview is thus:

This method completely negates the need for an .end class and means you can just keep on adding list items without ever having to worry about things breaking!
July 15th, 2010 by Harry Roberts in Web Development.





Russell Bishop said on July 15, 2010 at 1:41 pm
Thanks for the article, Harry!
I’ve often found myself using scripts to add ‘end’ classes onto my items, and only recently realised the negative margin fix, which works a treat!
The only thing to worry about then is if you have elements within your ‘grid’ that aren’t supposed to be part of that flow, but use the positioning of the container (ul) to base their own properties from.
Garrett Winder said on July 20, 2010 at 4:05 am
Great article!
Recently I’ve just been using stuff like #images li:nth-of-type(4n) { margin-right: 0; } instead of .end, etc. To make it work w/ IE I throw in Keith Clark’s ie-css3.js file.
Jansen Tolle said on August 17, 2010 at 10:22 pm
Very smart! I like this approach – it’s very clean and semantic.