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.
A card can be short. Or it can
A card can be short. Or it can carry a good deal more text, which makes it taller t
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
A card can be short. Or it can carry a
A card can be short. Or it can carry a good deal more text, which makes it taller than the c
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
A card can be short. Or it can carry a good deal
A card can be short. Or it can carry a good deal more text, which makes it taller than the card besid
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
A card can be short. Or it can carry a good deal more tex
A card can be short. Or it can carry a good deal more text, which makes it taller than the card beside it, and
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
A card can be short. Or it can carry a good deal more text, which
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. columnsfixes the number of columns. Without it as many fit asminWidth(px) allows, and the count follows the container as it resizes.gapis 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 (
widthandheight, 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: masonrywhich is not widely supported yet. If that changes this can shrink to a few lines of CSS.
Usage
import { BlessMasonry } from "blessing-ui";API
Props
| Name | Type | Default | Description |
|---|---|---|---|
items* | T[] | — | |
rowKey | ((item: T, index: number) => string | number) | — | |
columns | number | — | a fixed number of columns; leave out to fit as many as `minWidth` allows |
minWidth | number | 240 | narrowest a column may get, in px, when `columns` is not set |
gap | number | 16 | space between cards, in px |
label | string | — |
Slots
| Name | Scope | Description |
|---|---|---|
default | { item: T; index: number; } |