update 2015
I noticed there was a slight issue with the previous set-up. In that keeping the articles within the first placeholder doubled up the margins. This led to slight inconsistencies when resizing the page across each article wrap point.
This updated fiddle gets rid of those issues:
http://jsfiddle.net/C4fbX/4/
However, this kind of layout could very likely be achieved in a nicer way with Flexbox these days.
https://css-tricks.com/snippets/css/a-guide-to-flexbox/
answered 2012
The following method works, however it is a little mad. It's rather annoying that this isn't something that CSS can handle easily:
fiddle
http://jsfiddle.net/C4fbX/
method
This method works on the basic idea posted by Nick, and uses this method to then position a left floating system... which fixes the problem where you always end up with center aligned elements within the centered container.
So instead of:
[][][][]
[][]
You get:
[][][][]
[][]
The following stipulations are required:
- You need to include as many placeholder elements as you have articles. The method uses these to calculate where to indent the articles from. This also means that the width and horizontal margin/padding of the placeholder elements has to match that of the articles.
- The articles need to be placed within the first placeholder.
- In order to get around the whitespace issue with regard to
inline-block
, the font-size
and line-height
are zeroed out, which means they have to be manually set again for the articles. You can get around this by removing these zeroed attributes from the css and just making sure your placeholder markup doesn't have any whitespace.
- I very much doubt this will work in older browsers - but seems to in the current modern ones.
- Relying on position absolute means that your articles wont take up their usual space in the document, so you'd have to account for this in your design or set a fixed height on your
#container
element
mark up
<div class="container">
<div class="placeholder">
<div class="position">
<article>a</article>
<article>b</article>
<article>c</article>
<article>d</article>
<article>e</article>
</div>
</div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
<div class="placeholder"></div>
</div>
css
.container {
font-size: 0;
line-height: 0;
text-align: center;
}
.placeholder {
display: inline-block;
width: 200px;
margin-left: 5px;
margin-right: 5px;
height: 0px;
}
.position {
position: absolute;
}
article {
font-size: 12pt;
line-height: 1.2em;
float: left;
margin: 5px;
width: 200px;
height: 100px;
background: red;
}