There's a more straightforward approach to achieve this using only pure CSS: utilizing the properties column-count
and column-width
.
Check out this live demo on JSFiddle (https://jsfiddle.net/3z73obt0/1/)
Below is the code snippet:
#wrapper {
-moz-column-count: 2;
-webkit-column-count: 2;
column-count: 2;
-moz-column-gap: 10px;
-webkit-column-gap: 10px;
column-gap: 10px;
}
#wrapper > div:nth-child(n+2) {
margin-top:10px;
}
#wrapper > div:nth-child(1) {
height: 150px;
width:100%;
background-color:#8ebce5;
}
#wrapper > div:nth-child(2) {
height: 150px;
width:100%;
background-color:#3a2313;
}
#wrapper > div:nth-child(3) {
height: 100px;
width:100%;
background-color:peru;
}
#wrapper > div:nth-child(4) {
height: 100px;
width:100%;
background-color:#BB3579;
}
#wrapper > div:nth-child(5) {
height: 100px;
width:100%;
background-color:#EAC243;
}
<div id="wrapper">
<div></div>
<div></div>
<div></div>
<div></div>
<div></div>
</div>
If you'd still prefer using a library, consider exploring the isotope library for a similar layout. Learn more about it here:
Here's a beautiful masonry layout created with isotope on Codepen: http://codepen.io/desandro/pen/mIkhq