Introduction

Masonry is a dynamic grid layout plugin for jQuery. Think of it as the flip-side of CSS floats. Whereas floating arranges elements horizontally then vertically, Masonry arranges elements vertically, positioning each element in the next open spot in the grid. The result minimizes vertical gaps between elements of varying height, just like a mason fitting stones in a wall.

Getting started

Markup

Masonry works on a container element with a group of similar child items.

<div id="container">
  <div class="item">...</div>
  <div class="item">...</div>
  <div class="item">...</div>
  ...
</div>

Add jQuery and the Masonry script. Masonry requires jQuery v1.4.0 and greater.

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.6.1/jquery.min.js"></script>
<script src="/path/to/jquery.masonry.min.js"></script>

CSS

All sizing of items is handled by your CSS. Items should be floated.

.item {
  width: 220px;
  margin: 10px;
  float: left;
}

Script

It is recommended you specify an itemSelector and columnWidth. There are a number of other options you can specify.

$(function(){
  $('#container').masonry({
    // options
    itemSelector : '.item',
    columnWidth : 240
  });
});

That’s it!

See Demo: Basic multi-column

imagesLoaded plugin

If your content contains any images, you’ll want to ensure that Masonry is triggered after all the images your content has loaded. The included imagesLoaded plugin makes this easy.

var $container = $('#container');
$container.imagesLoaded(function(){
  $container.masonry({
    itemSelector : '.item',
    columnWidth : 240
  });
});

See Demo: Images

Code repository

This project lives on GitHub at github.com/desandro/masonry. There you can grab the latest code and follow development.

Acknowledgments

Changelog

v2.1: 9 Dec 2011
Pass in a function for columnWidth for easier fluid layouts
v2.0: May 2011
Complete re-write. Uses $.Mason constructor, like jQuery UI widget
Add options gutterWidth, isFitWidth for centering layout, isRTL for right-to-left layout
Add all new methods like appended, and reload
Remove filtering demos
v1.3: 3 Sep 2010
Revamped appendedContent to work with container elements. Plays nice with latest Infinite Scroll.
Revised layout for documentation to allow for more pages.
v1.2: 12 Jun 2010
Support for filtering added
v1.1: 29 Apr 2010
Add animation
v1.0: 7 Dec 2009
Multi-column width support
Appending elements and Infinite Scroll support
Less obstrusive layout. No inserting additional markup.
Automatically binds event to window resizing
v0.4: 14 Jun 2009
Better fluid rearrangement support
v0.1: Feb 2009
Original release

License

jQuery Masonry is licensed under the MIT license. It may be used for personal and commercial applications.

The MIT License

Copyright © 2011 David DeSandro

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.