Minimum recommended configuration
Configuring Masonry is fairly easy. Simply attach the .masonry()
method to the wrapping container element. Depending on the layout, you’ll most likely need to specify one option.
For layouts with elements that span multiple widths:
$('#wrapper').masonry({ columnWidth: 200 });
For layouts with elements that span the same width:
$('#wrapper').masonry({ singleMode: true });
All options
$('#wrapper').masonry({
singleMode: false,
columnWidth: 240,
itemSelector: '.box:visible',
resizeable: true,
animate: true,
animationOptions: {},
appendedContent: $('.new_content'),
saveOptions: true,
}, function() {}
);
No need to worry about how many columns, or width of gutters, or how big the container is. Thanks to the wonders of jQuery’s outerWidth()
and innerWidth()
methods, Masonry appropriately accounts for the space of any margin, padding, and border-width set with CSS.
Project Home
This project lives on GitHub at github.com/desandro/masonry, where you can follow progress and download all these demos and documentation, and grab the latest version of the script.
There you can find an issue tracker where you can look through other people's resolved issues or submit a new one for yourself. If you're still having trouble, try looking over the FAQ.
Window Resizing
By default, Masonry binds a call to the browser window for resizing. But the rearrangement script is triggered only when the layout adds or loses a column, so you don’t have to worry about Masonry slowing down window resizes, or with fixed layouts.
Handling Images & Other Media
Since Masonry measures the height of the elements when placing them, you will need to account for images and other media that haven’t loaded yet. For images, the best method is to specify the width and height of images inline.
<img src="img_file.jpg" width="280" height="160" />
If you’re using a PHP-based CMS, you can use the getimagesize function.
If this is not possible or if you’re dealing with @font-face fonts, another option is to call Masonry after all media has loaded. This is done by calling the function inside of $(window).load()
instead of $(document).ready()
.
$(window).load(function(){
$('#wrapper').masonry({ columnWidth: 200 });
});
Resolving Anchor Links
Since Masonry relies on absolute positioning, any anchor links that occur within or after the wrapping element will not work when the page first loads. The following script is one solution.
$(window).load(function(){
if ( window.location.hash ) {
var destination = $( window.location.hash ).offset().top;
$('html:not(:animated),body:not(:animated)').scrollTop( destination );
}
});
This will set the window of the document to the appropriate place. These couple lines were taken from Cedric Dugas’s anchorAnything.
Examples in the Wild
In addition to the examples below, see delicious links tagged with jquerymasonry and jQuery Masonry Collection on Ember for screenshots.
Changelog
- v1.2 12 Jun 2010
- Support for filtering added
- Full animation options 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 dual-licensed under GPL and MIT, just like jQuery itself. You can use it for both personal and commercial applications.