If you look at the pinterest.com homepage , you will find out that each section gets a top and left property. I think they calculate and set the top and left property.
This is just an approach to get your desired look.
Instead of having one row and adding all divs inside, you can have x columns in that row and programmatically add divs in each column.
EX: Let's say I set 4 columns x1, x2, x3 and x4 and had about 9 divs. I started the loop so that divs were added in this order: d1 β x1, d2-> x2, d3-> x3, d4-> x4, d5-> x1 ..... and so on.
Check out the code below. I used an array for heights to show that this works for different heights.
var arr = [100, 200, 300, 400, 200, 100, 300, 500]; var colors = ["red","green","blue", "yellow","golden", "orange", "maroon"] var divs = 13; for (var i = 0; i < divs; i++) { var ht = arr[Math.floor(Math.random() * arr.length)]; var clr = colors[Math.floor(Math.random() * colors.length)]; var rem = i%4; $("#wrapper .box"+rem).append("<div >random text</div>"); $(".box"+rem+" div:last").css({ "height": ht + "px", "background":clr+"" }); }
Fiddle
Note: The js code to add will depend on your code for generating the div. I just used this demo.
source share