Appendix

imagesLoaded

Unloaded images can throw off Masonry layouts and cause item elements to overlap. imagesLoaded resolves this issue. imagesLoaded works by triggering a callback after all child images have been loaded.

var container = document.querySelector('#container');
var msnry;
// initialize Masonry after all images have loaded
imagesLoaded( container, function() {
  msnry = new Masonry( container );
});
// or with jQuery
var $container = $('#container');
// initialize Masonry after all images have loaded  
$container.imagesLoaded( function() {
  $container.masonry();
});

Or initialize Masonry first, then trigger layout after images have loaded.

// initialize Masonry
var msnry = new Masonry( container );
// layout Masonry again after all images have loaded
imagesLoaded( container, function() {
  msnry.layout();
});
// or with jQuery
// initialize Masonry
var $container = $('#container').masonry();
// layout Masonry again after all images have loaded
$container.imagesLoaded( function() {
  $container.masonry();
});

Web fonts

Like images, unloaded web fonts can throw off Masonry. To resolve this, trigger layout after fonts have been loaded. Both Typekit and Google WebFont Loader provide font events to control scripts based on how fonts are loaded.

Typekit

Try the script below when using Masonry on a page with Typekit. This will trigger Masonry when the document is ready and again when fonts have loaded. Be sure to remove Typekit’s default script, try{Typekit.load();}catch(e){}.

var msnry;

function triggerMasonry() {
  // don't proceed if masonry has not been initialized
  if ( !msnry ) {
    return;
  }
  msnry.layout();
}
// initialize masonry on document ready
docReady( function() {
  var container = document.querySelector('#container');
  msnry = new Masonry( container, {
    // options...
  });
});
// trigger masonry when fonts have loaded
Typekit.load({
  active: triggerMasonry,
  inactive: triggerMasonry
});
// or with jQuery
var $container;

function triggerMasonry() {
  // don't proceed if $container has not been selected
  if ( !$container ) {
    return;
  }
  // init Masonry
  $container.masonry({
    // options...
  });
}
// trigger masonry on document ready
$(function(){
  $container = $('#container');
  triggerMasonry();
});
// trigger masonry when fonts have loaded
Typekit.load({
  active: triggerMasonry,
  inactive: triggerMasonry
});

Component libraries

Masonry includes several component libraries. You might have seen these used in the example code. You can use some of these libraries in your own code.

docReady

docReady triggers initialization logic when the document is ready, just like jQuery's $(document).ready(). docReady is used to initialize all the demos in these docs.

docReady( function() {
  // document is ready, let's do some fun stuff!
  var container = document.querySelector('#container');
  var msnry = new Masonry( container );
});

classie

classie has class helper functions. classie is not included with Masonry.

classie.has( element, 'my-class' ) // returns true/false
classie.add( element, 'my-new-class' ) // add new class
classie.remove( element, 'my-unwanted-class' ) // remove class
classie.toggle( element, 'my-class' ) // toggle class

eventie

Eventie makes event binding in IE8 legit.

var elem = document.querySelector('#my-elem');
function onElemClick( event ) {
  console.log( event.type + ' just happened on #' + event.target.id );
  // -> click just happened on #my-elem
}
// bind it
eventie.bind( elem, 'click', onElemClick );
// unbind it
eventie.unbind( elem, 'click', onElemClick );

Submitting issues

Reduced test case required

All bug reports and problem issues require a reduced test case. Create one by forking any one of the CodePen examples from the docs.

Providing a reduced test case is the best way to get your issue addressed. Without a reduced test case, your issue may be closed.

Upgrading from v2

RequireJS

Masonry supports RequireJS.

You can require masonry.pkgd.js.

requirejs( [
  'path/to/masonry.pkgd.js',
], function( Masonry ) {
  new Masonry( '#container', {...});
});

To use Masonry as a jQuery plugin with RequireJS and masonry.pkgd.js, you need to run jQuery bridget.

// require the require function
requirejs( [ 'require', 'jquery', 'path/to/masonry.pkgd.js' ],
  function( require, $, Masonry ) {
    // require jquery-bridget, it's included in masonry.pkgd.js
    require( [ 'jquery-bridget/jquery.bridget' ],
    function() {
      // make Masonry a jQuery plugin
      $.bridget( 'masonry', Masonry );
      // now you can use $().masonry()
      $('#container').masonry({...});
    }
  );
});

Or, you can manage dependencies with Bower. Set baseUrl to bower_components and set a path config for all your application code.

requirejs.config({
  baseUrl: 'bower_components/',
  paths: {
    app: '../'
  }
});

requirejs( [
  'masonry/js/masonry',
  'app/my-component.js'
], function( Masonry, myComp ) {
  new Masonry( '#container', {...});
});

You can require Bower dependencies and use Masonry as a jQuery plugin with jQuery Bridget.

requirejs.config({
  baseUrl: '../bower_components',
  paths: {
    jquery: 'jquery/jquery'
  }
});

requirejs( [
    'jquery',
    'masonry/js/masonry',
    'jquery-bridget/jquery.bridget'
  ],
  function( $, Masonry ) {
    // make Masonry a jQuery plugin
    $.bridget( 'masonry', Masonry );
    // now you can use $().masonry()
    $('#container').masonry({...});
});

Extra examples

Additional resources