Question: I have something like this:
<http://s9.postimg.org/wwizuwnq7/Untitled_1.png>

And if you see, the divs (Where I marked in green) have a space of some pixel.
And i want if there is a 0-20 pixel space between divs, to order them like this:
<http://s23.postimg.org/ky2htcpt7/image.png>

So, i started to do this on javascript and i dont know to to continue..

```
var position = new Array();
$(".post").each(function(){
    position[$(this).attr("id")] = $(this).offset().top - $(window).scrollTop();
});

```

now i have all the position of all the divs, and now i need to check where divs have a space of 0 - 20 pixel, and then i want to take down the higher block.

I not sure if this is the good way, and if now, i need another idea..

Thanks!

Answer: 
I managed to find a method!

```
var position = new Array();
$(".hblocks").each(function(){
    position[$(this).attr("id")] = $(this).offset().top;
});

$.each(position, function(key, value) {
    $.each(position, function(key2, value2) {
        var space = value2 - value;
        if (space <= 20 && space >= -20 && space != 0)
        {
            var finalSpace = Math.max(value, value2);
            var spaceplus = space + 28;
            if (finalSpace != value)
            {
                $("#" + key).css("margin-top",spaceplus + "px");
            }
            else
            {
                $("#" + key2).css("margin-top",spaceplus + "px");
            }
        }
    });
});

```
Your answer would be some king of javascript+css coding to verify height of elements .. work on em then re-arrange them.

Stop trying to figure out by yourself, try using [Masonry](http://masonry.desandro.com/) or [jQueryEqualHeight](http://css-tricks.com/equal-height-blocks-in-rows/) explained on CSSTrick.

**What is Masonry?**

> 
> Masonry is a JavaScript grid layout library. It works by placing
>  elements in optimal position based on available vertical space, sort
>  of like a mason fitting stones in a wall. You’ve probably seen it in
>  use all over the Internet.
> 
> 
>