How can I put items of different sizes in one page avoiding too many gaps between them?

I have a set of rectangles of different sizes and I want to put them next to each other so there are as little gaps in there as possible:
enter image description here

but for:

<div class="box" >
    <div class="item item-001" ></div>
    <div class="item item-003" ></div>
    <div class="item item-005" ></div>
    <div class="item item-002" ></div>
    <div class="item item-004" ></div>
    <div class="item item-001" ></div>
    <div class="item item-001" ></div>
    <div class="item item-003" ></div>
    <div class="item item-004" ></div>
    <div class="item item-001" ></div>
</div>

and

div.box {
    width: 100%;
    height: 100%;
    display: block;
    border: 1px solid #EEE;
    padding: 3px;
}

div.item {
    display: inline-block;
    border: 1px solid orange;
    margin: 3px;
    width: 100px;
}
div.item-001 {
    height: 100px;
}
div.item-002 {
    height: 150px;
}
div.item-003 {
    height: 50px;
}
div.item-004 {
    height: 250px;
}
div.item-005 {
    height: 350px;
}

this is what I get:

enter image description here

Is there a way I could achieve that result with plain css? I want to avoid css3 and javascript and I want the result to run on older browsers. If that is not possible, then I’d like to know if it is possible with css3 and if not, then I guess I’ll try javascript.

link to fiddle: http://jsfiddle.net/hamidsafdari/aa10xzzt/1/


Source: css3

Leave a Reply