Back to elements
ElementLayoutFree

Masonry Grid React component

A masonry layout for tiles of different heights: each tile drops into the shortest column while keeping left-to-right order, the column count follows the container's width, and tiles spring to their new places when it changes.

When to use the Masonry Grid component

Use Masonry Grid for collections whose items have different heights: photo galleries, mood boards, testimonial walls, notes and pins. Nothing gets cropped to a fixed row height and there are no gaps under short items.

For items of equal size a plain CSS grid is simpler; for a curated layout with deliberate big and small tiles, use Bento Grid.

Installation

Add the Masonry Grid component to your project with the shadcn CLI. It lands in components/masonry-grid.tsx as plain React + Tailwind CSS source you own and can edit.

Install
Copied!
npx shadcn@latest add https://reactframe.com/r/masonry-grid.json

Or copy the source by hand and install its dependencies:

Install
Copied!
npm install motion clsx tailwind-merge

Props

<MasonryGrid /> accepts 7 props, all optional.

  • children

    The tiles. Any content works; heights are measured.

    React.ReactNode
  • columns

    A fixed number of columns. Leave out to fit as many as minColumnWidth allows.

    number
  • minColumnWidth
    number
    Default: 200
  • maxColumns
    number
    Default: 4
  • gap
    number
    Default: 12
  • animateIn

    Tiles fade up one after another the first time they're placed.

    boolean
    Default: true
  • className
    string

FAQ

Does it keep the order of my items?
Yes. Items are taken in order and each goes into the shortest column, so the first items stay near the top and the grid reads left to right, unlike CSS columns which fill top to bottom.
What happens when images load or the window resizes?
Every tile's height and the container's width are watched, so the layout recalculates and tiles spring to their new positions.
How many columns will it show?
As many as fit at minColumnWidth, up to maxColumns. Pass columns to fix the number instead.

More Layout components

Bento Grid
Free
Panel
Free
Sticky Header
Free
Toolbar
Free
Section Container
Free
Scroll Area
Free