As you can see in the image every box has a different height and there are some boxes with double width.
Is it possible to create a masonry-style layout only with CSS?
As you can see in the image every box has a different height and there are some boxes with double width.
Is it possible to create a masonry-style layout only with CSS?
With css3 support you could do this:
.container {
-moz-column-count: 2;
-moz-column-gap: 10px;
-webkit-column-count: 2;
-webkit-column-gap: 10px;
column-count: 2;
column-gap: 10px;
width: 360px;
}
.container div {
display: inline-block;
width: 100%;
background-color: red;
}
With no css3 support, you have to resort to js unfortunately.
I'm working on a site right now with the same kind of layout, two columns with the occasional double-wide box. What I do is just separate the double-wide from the rest of the content. For example:
<div class="two-columns">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
<div class="double-wide">
</div>
<div class="two-columns">
<div></div>
<div></div>
<div></div>
<div></div>
</div>
Then you can apply the CSS3 column solution to just the two-columns
class. If you need to support IE9 you'll sadly need to add a JavaScript fallback.