Skip to content
Calm motion: animations on this site only fade, with no movement

Masonry ​

Variable-height cards in columns

A wall of cards of different heights (photos, notes, posts) packed so no column has holes. For items of equal size use a plain CSS grid. For a feed read top to bottom, a DataView.

Basic
Card 1

A card can be short. Or it can

Card 2

A card can be short. Or it can carry a good deal more text, which makes it taller t

Card 3

A card can be short. Or it can carry a good deal more text, which makes it taller than the card beside it, and that is exactly when colu

Card 4

A card can be short. Or it can carry a

Card 5

A card can be short. Or it can carry a good deal more text, which makes it taller than the c

Card 6

A card can be short. Or it can carry a good deal more text, which makes it taller than the card beside it, and that is exactly when columns of eq

Card 7

A card can be short. Or it can carry a good deal

Card 8

A card can be short. Or it can carry a good deal more text, which makes it taller than the card besid

Card 9

A card can be short. Or it can carry a good deal more text, which makes it taller than the card beside it, and that is exactly when columns of equal rows

Card 10

A card can be short. Or it can carry a good deal more tex

Card 11

A card can be short. Or it can carry a good deal more text, which makes it taller than the card beside it, and

Card 12

A card can be short. Or it can carry a good deal more text, which makes it taller than the card beside it, and that is exactly when columns of equal rows leave gap

Card 13

A card can be short. Or it can carry a good deal more text, which

Card 14

A card can be short. Or it can carry a good deal more text, which makes it taller than the card beside it, and that is

  • Each card goes into the shortest column, in the order of items. The DOM stays in that same order, so reading, tabbing and screen readers follow the list. Only the placement is spread over columns.
  • columns fixes the number of columns. Without it as many fit as minWidth (px) allows, and the count follows the container as it resizes. gap is the space between cards in px.
  • Cards are measured, and a card that changes height (an image that loads, text that wraps differently) moves the ones after it. Give images their size (width and height, or an aspect ratio) to avoid jumps.
  • Before the script runs, and without JavaScript, the cards are a normal single column, so nothing is hidden. Under RTL the columns start on the right. Movement between positions is a short slide, off under prefers-reduced-motion.
  • It lays out with measurements, not the CSS grid-template-rows: masonry which is not widely supported yet. If that changes this can shrink to a few lines of CSS.

Usage ​

ts
import { BlessMasonry } from "blessing-ui";

API ​

Props

NameTypeDefaultDescription
items*T[]—
rowKey((item: T, index: number) => string | number)—
columnsnumber—a fixed number of columns; leave out to fit as many as `minWidth` allows
minWidthnumber240narrowest a column may get, in px, when `columns` is not set
gapnumber16space between cards, in px
labelstring—

Slots

NameScopeDescription
default{ item: T; index: number; }

Released under the MIT License. Character names and artwork belong to their owners.