Masonry Grid for Astro
A responsive Astro masonry layout for cards, images, and other content.
Installation
bunx --bun shadcn@latest add @gnostic/masonry-gridnpx shadcn@latest add @gnostic/masonry-gridpnpm dlx shadcn@latest add @gnostic/masonry-gridCopy the files
src/components/ui/masonry-grid/index.tssrc/components/ui/masonry-grid/types.tssrc/components/ui/masonry-grid/masonry-grid.astrosrc/components/ui/masonry-grid/masonry-grid.ts
Usage
---
import { Image } from "astro:assets"
import { MasonryGrid } from "@/components/ui/masonry-grid"
import landscape from "@/assets/images/winter-landscape-theta.png"
import clouds from "@/assets/images/clouds-around-water.png"
---
<MasonryGrid gap="0.75rem">
<Image
src={landscape}
alt="Pink trees in a snowy landscape beneath two planets"
width={640}
height={800}
fit="cover"
class="h-auto w-full rounded-lg"
/>
<Image
src={clouds}
alt="Clouds around a circular lake"
width={640}
height={400}
fit="cover"
class="h-auto w-full rounded-lg"
/>
</MasonryGrid>
Use different image heights to show the offset columns. This example crops
the images to portrait, square, and landscape shapes. Keep h-auto w-full
on each image so its displayed height follows its dimensions. The top example
uses at most two columns. Its final image crop accounts for the extra gap in
the right column, so both columns end at the same height.
Each direct child is one item. Use a wrapper when several elements belong to
one item. Cards, images, links, and other Astro components can use the same
layout. Items keep their natural height. Use gap for spacing between items;
keep outer margins and position styles off the item roots.
The component places each item in the shortest column. Equal columns fill from the inline start, including in right-to-left layouts. The script measures items again when the container, an image, or other content changes size. It also handles items added to or removed from the slot container.
The default is one column below 30rem, two from 30rem, and four from 48rem.
These widths refer to the component container. Set minCols to a positive integer.
For example, minCols={6} gives the layout six columns at every width.
Use the preview width controls to check a smaller container.
Cards
Cards with different amounts of content use the same layout. Each card keeps its natural height.
Images with lightboxes
Use LightboxGridItem inside MasonryGrid. Set the visible shape of each tile with
image.thumbnail.class, for example "aspect-4/5". The enlarged image uses the full source by default.
See the masonry lightbox example.
LightboxGrid layout="masonry" uses this same component internally.
Reading order and fallback
Source order stays unchanged for keyboard and screen reader access. Visual positions depend on item height. Use a normal grid for content that must read in strict rows. Without JavaScript, items use a responsive grid with natural heights. Supply image dimensions to reduce layout movement during loading.
API Reference
MasonryGrid accepts native div attributes and a default slot.
Use class for the outer container. Use style to override --masonry-gap.
MasonryGridProps
minCols?number
Minimum column count as a positive integer. The layout uses two columns from 30rem and four from 48rem, measured against the component width.
number1gap?string
Space between items as a CSS length.
string'1rem'The registry source includes the Astro component and its browser script.
Theme and layout
Use the outer class for page width and spacing. Set the gap through the component prop or the --masonry-gap variable. Keep outer margins and positioning off the item roots so measurements stay accurate. Place Card or Lightbox content inside the grid, and check the reading order at each width.
The root is a normal HTML element. The component attaches its factory on script
load and astro:page-load, then calls destroy() before an Astro page swap.
For direct DOM changes, import createMasonryGrid from the component index.
Destroy the returned controller before removing the root.




