Grid Rows Masonry
Overview

Grid Rows Masonry is a ponyfill for the CSS masonry layout, designed for browsers that don’t yet support it. It simulates the effect of grid-template-rows: masonry;
by reordering child elements and applying negative top margins, ensuring each item neatly aligns under the one above it while preserving the grid’s row gap.
Installation
Install via npm:
npm install grid-rows-masonry
Usage
- Define a grid container with multiple columns:
<div id="my-grid" style="display: grid; grid-template-columns: repeat(3, 1fr);">
...
</div>
- Import the module and initialize it:
import { Masonry } from "grid-rows-masonry";
const el = document.getElementById("my-grid");
const masonry = new Masonry(el);
- To reset the layout, call the destroy() method:
masonry.destroy();
Usage with React
To use the Masonry React component exported from grid-rows-masonry/react
:
- Import the component:
import { Masonry } from "grid-rows-masonry/react";
- Use the component in your React application, ensuring to set the
display: grid
style:
const MyGrid = () => {
return (
<Masonry style={{ display: "grid", gridTemplateColumns: "repeat(3, 1fr)" }}>
...
</Masonry>
);
};
This component will automatically handle the masonry layout for its children.
Example
Grid layout with the Grid Rows Masonry ponyfill applied.
