Minimum recommended configuration
Configuring Masonry is fairly easy. Simply call the .masonry()
method on the container element. Depending on the layout, you’ll most likely need to specify one option.
For layouts with elements that span multiple widths, like the basic multi-column example:
$('#wrapper').masonry({ columnWidth: 200 });
For layouts with elements that span the same width, like the basic single-column example:
$('#wrapper').masonry({ singleMode: true });
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.
This plugin is designed to progressively enhance the layout, so ‘brick’ elements should be floated if JavaScript is disabled. Floating also ensures that the bricks width will be measured correctly.
Plugin options
$('#wrapper').masonry({
singleMode: false,
columnWidth: 240,
itemSelector: '.box:visible',
resizeable: true,
animate: true,
animationOptions: {},
appendedContent: $('.new_content'),
saveOptions: true,
}, function() {}
);
Code repository
This project lives on GitHub at github.com/desandro/masonry, where you can follow progress, download all this documentation, grab the latest version of the script, or fork this project and make it better yourself.
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.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
Acknowledgments
- Paul Irish for his Infinite Scroll plugin, which served as a template I used to produce a plugin of my own. The
appendedContent
option was design to work specifically with Infinite Scroll. Infinite Scroll is packaged with these docs.
- Louis-Rémi Babé and Paul Irish for the jQuery SmartResize plugin. This adds a special smartresize event to jQuery, so window resize events do not have to be executed on every frame. This plugin is used within jQuery Masonry.
License
jQuery Masonry is licensed under the MIT license, just like jQuery itself. It may be used for personal and commercial applications.
The MIT License
Copyright © 2010 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.