Skip to content

Masonry Grid

A masonry layout for React that keeps source order: a CSS grid with measured row spans, so keyboard and screen reader order match the visual flow.

src/registry/ui/masonry-grid.tsx/r/masonry-grid.json

Installation

bash
npx shadcn@latest add @minidev/masonry-grid

MiniDev UI is in the shadcn registry directory, so the short form works with no setup. Older CLI versions can use the full URL: npx shadcn@latest add https://ui.minidev.pro/r/masonry-grid.json

Usage

tsx
import { MasonryGrid } from "@/components/ui/masonry-grid"

export function Example() {
  return (
    <MasonryGrid />
  )
}

API reference

MasonryGrid props
PropTypeDefault
childrenOne element per tile. Keyed children keep their measured height when reordered.React.ReactNode-
minColumnWidthMinimum column width in pixels.number180
gapGap between tiles in pixels, both directions. Keep it on the 4px grid.number12
classNameClasses for the grid.string-

Also accepts every prop of <div>, passed through to the root element.

When to use

A CSS grid with tiny rows where each item spans as many rows as its measured height. Items flow left to right, so keyboard and screen reader order match what you see, and the column count follows the container width. Use it for galleries, notes and testimonials; for equal-height cards use a plain grid. Each child is measured, so pass one element per item (no fragments). Other div props (aria-label, role, id) go on the grid element.

Accessibility

  • DOM order matches visual order, so keyboard and screen reader users meet tiles in the order sighted users read them.
  • The grid and each tile wrapper are plain <div>s. Other div props go on the grid, so name it with aria-label or aria-labelledby when it needs a name. Keep role="list" off unless every child also has role="listitem".

Frequently asked questions

How is this different from CSS columns?

CSS columns fills the first column top to bottom, then the next, so Tab order jumps around the screen. MasonryGrid keeps items left to right, row by row. It measures each tile and sets grid-row-end: span N on rows of 4px.

Why do tiles overlap before the page hydrates?

Heights are measured in the browser, in useLayoutEffect. Server HTML has every tile spanning one 4px row, so tiles stack on top of each other until React runs. On a server-rendered page, keep the grid below the fold or render it only after mount.

Do images that load late break the layout?

No. A ResizeObserver watches each tile and updates its span when the height changes. Set width and height on images anyway so tiles do not jump.

Examples

Note cards

Cards of different lengths pack into columns while keeping source order, so tabbing through them still reads left to right.

Compact media tiles

Narrow columns and a small gap for a photo wall; each tile keeps its own aspect ratio.

All layout components