css float elements with unequal heights left and up in grid css float elements with unequal heights left and up in grid javascript javascript

css float elements with unequal heights left and up in grid


You can use the popular library Masonry.

A dynamic layout plugin for jQuery The flip-side of CSS floats

Here is a code example...

$('#container').masonry({  itemSelector: '.box'});

Here is the source on Github and an interview with David Desandro on the Shoptalk podcast.

For folks that aren't using jQuery, note that there's also Vanilla Masonry which is the framework-free version.

Tip: Make sure the parent container has position:relative so all the content is bound to your container.


Since you are already using jquery, jquery masonry might interest you: http://masonry.desandro.com


Well, if you only aim to support the most modern browser, the CSS3 multi-column layout could help. One problem with this approach is that it will not keep the same order, but you can play with the order in the HTML (or Javascript).

I added a container around your spans called #container.

#container {     -webkit-column-width: 100px;     -moz-column-width: 100px;     column-width: 100px; }

jsFiddle Demo