Convert Figma logo to code with AI

desandro logomasonry

:love_hotel: Cascading grid layout plugin

16,381
2,106
16,381
83

Top Related Projects

4,128

:bento: Gapless, draggable grid layouts

39,523

Most modern mobile touch slider with hardware accelerated transitions

2,367

Categorize, sort, and filter a responsive grid of items

10,773

Infinite responsive, sortable, filterable and draggable layouts

Quick Overview

Masonry is a JavaScript grid layout library that arranges elements vertically, positioning them into optimal positions based on available horizontal space. It creates a dynamic, cascading grid layout that's particularly useful for image galleries, article listings, or any content that benefits from a Pinterest-style layout.

Pros

  • Flexible and responsive design that adapts to various screen sizes
  • Smooth animations for a polished user experience
  • Lightweight and easy to implement
  • Supports multiple columning methods

Cons

  • Can be processor-intensive with a large number of items
  • Requires careful consideration of image sizes for optimal layout
  • May not be suitable for content that needs to maintain a specific order
  • Limited built-in customization options

Code Examples

  1. Basic initialization:
var elem = document.querySelector('.grid');
var msnry = new Masonry( elem, {
  // options
  itemSelector: '.grid-item',
  columnWidth: 200
});
  1. Initializing with jQuery:
$('.grid').masonry({
  itemSelector: '.grid-item',
  columnWidth: '.grid-sizer',
  percentPosition: true
});
  1. Adding and removing items:
// Add items
var elems = [getItemElement(), getItemElement(), getItemElement()];
msnry.appended(elems);

// Remove an item
msnry.remove(elem);

Getting Started

  1. Install Masonry via npm:
npm install masonry-layout
  1. Include Masonry in your HTML:
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>
  1. Set up your HTML structure:
<div class="grid">
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  ...
</div>
  1. Initialize Masonry in your JavaScript:
var grid = document.querySelector('.grid');
var msnry = new Masonry(grid, {
  itemSelector: '.grid-item',
  columnWidth: 200
});

Competitor Comparisons

4,128

:bento: Gapless, draggable grid layouts

Pros of Packery

  • Supports draggable items, allowing for interactive layouts
  • Offers more layout options, including vertical alignment and right-to-left ordering
  • Provides better support for responsive designs with percentage-based sizing

Cons of Packery

  • Larger file size, which may impact page load times
  • More complex API, potentially steeper learning curve for beginners
  • Commercial license required for some use cases, unlike Masonry's MIT license

Code Comparison

Masonry:

var grid = document.querySelector('.grid');
var msnry = new Masonry(grid, {
  itemSelector: '.grid-item',
  columnWidth: 200
});

Packery:

var pckry = new Packery('.grid', {
  itemSelector: '.grid-item',
  columnWidth: 200,
  isHorizontal: true,
  dragItemSelector: '.grid-item'
});

Both libraries use similar initialization syntax, but Packery offers additional options like isHorizontal and dragItemSelector for more advanced layouts and interactivity.

39,523

Most modern mobile touch slider with hardware accelerated transitions

Pros of Swiper

  • More versatile, supporting various types of sliders and carousels
  • Active development with frequent updates and new features
  • Extensive documentation and community support

Cons of Swiper

  • Larger file size and potentially higher performance overhead
  • Steeper learning curve due to more complex configuration options

Code Comparison

Masonry:

var grid = document.querySelector('.grid');
var msnry = new Masonry(grid, {
  itemSelector: '.grid-item',
  columnWidth: 200
});

Swiper:

var swiper = new Swiper('.swiper-container', {
  slidesPerView: 3,
  spaceBetween: 30,
  pagination: {
    el: '.swiper-pagination',
    clickable: true,
  },
});

Key Differences

Masonry is focused on creating grid layouts with a "masonry" effect, where elements are positioned optimally based on available vertical space. Swiper, on the other hand, is designed for creating touch-enabled sliders and carousels.

Masonry is ideal for image galleries or content grids where items have varying heights. Swiper excels in creating interactive slideshows, product carousels, and touch-friendly content sliders.

While Masonry offers a simpler API for its specific use case, Swiper provides a wider range of features and customization options, making it more suitable for complex slider requirements.

2,367

Categorize, sort, and filter a responsive grid of items

Pros of Shuffle

  • Built with modern JavaScript (ES6+) and TypeScript support
  • Includes built-in filtering and sorting capabilities
  • Smaller file size and better performance for large collections

Cons of Shuffle

  • Less widespread adoption and community support
  • Fewer layout options compared to Masonry's flexible grid system
  • May require more setup for basic use cases

Code Comparison

Masonry:

var grid = document.querySelector('.grid');
var msnry = new Masonry(grid, {
  itemSelector: '.grid-item',
  columnWidth: 200
});

Shuffle:

const shuffleInstance = new Shuffle(element, {
  itemSelector: '.shuffle-item',
  sizer: '.my-sizer-element'
});

Both libraries aim to create dynamic grid layouts, but Shuffle offers more modern features and better performance for complex scenarios. Masonry has a larger user base and simpler implementation for basic grid layouts. The choice between them depends on specific project requirements, such as the need for filtering/sorting or support for older browsers.

10,773

Infinite responsive, sortable, filterable and draggable layouts

Pros of Muuri

  • More feature-rich, including drag & drop functionality
  • Actively maintained with recent updates
  • Supports both vanilla JavaScript and React implementations

Cons of Muuri

  • Larger file size and potentially higher performance overhead
  • Steeper learning curve due to more complex API
  • Less widespread adoption compared to Masonry

Code Comparison

Masonry:

var grid = new Masonry('.grid', {
  itemSelector: '.grid-item',
  columnWidth: 200
});

Muuri:

var grid = new Muuri('.grid', {
  items: '.grid-item',
  dragEnabled: true,
  layout: {
    fillGaps: true,
    horizontal: false,
    alignRight: false,
    alignBottom: false,
    rounding: false
  }
});

Both libraries aim to create responsive, dynamic grid layouts, but Muuri offers more advanced features like drag and drop functionality. Masonry is simpler to implement and has been around longer, making it a popular choice for basic grid layouts. Muuri, on the other hand, provides more flexibility and interactivity options, but may require more setup and configuration. The choice between the two depends on the specific requirements of your project and the level of complexity you're willing to manage.

Convert Figma logo designs to code with AI

Visual Copilot

Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.

Try Visual Copilot

README

Masonry

Cascading grid layout library

Masonry works by placing elements in optimal position based on available vertical space, sort of like a mason fitting stones in a wall. You’ve probably seen it in use all over the Internet.

See masonry.desandro.com for complete docs and demos.

Install

Download

CDN

Link directly to Masonry files on unpkg.

<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.js"></script>
<!-- or -->
<script src="https://unpkg.com/masonry-layout@4/dist/masonry.pkgd.min.js"></script>

Package managers

npm: npm install masonry-layout --save

Bower: bower install masonry-layout --save

Support Masonry development

Masonry has been actively maintained and improved upon for 8 years, with 900 GitHub issues closed. Please consider supporting its development by purchasing a license for one of Metafizzy's commercial libraries.

Initialize

With jQuery

$('.grid').masonry({
  // options...
  itemSelector: '.grid-item',
  columnWidth: 200
});

With vanilla JavaScript

// vanilla JS
// init with element
var grid = document.querySelector('.grid');
var msnry = new Masonry( grid, {
  // options...
  itemSelector: '.grid-item',
  columnWidth: 200
});

// init with selector
var msnry = new Masonry( '.grid', {
  // options...
});

With HTML

Add a data-masonry attribute to your element. Options can be set in JSON in the value.

<div class="grid" data-masonry='{ "itemSelector": ".grid-item", "columnWidth": 200 }'>
  <div class="grid-item"></div>
  <div class="grid-item"></div>
  ...
</div>

License

Masonry is released under the MIT license. Have at it.


Made by David DeSandro

NPM DownloadsLast 30 Days