Getting Started
Masonry Grid is a fast, lightweight, and responsive masonry grid layout library. This guide will help you get started with Masonry Grid in your project.
Installation
Section titled “Installation”-
Install package by running the following command in your terminal:
pnpm add @masonry-grid/vanillayarn add @masonry-grid/vanillanpm install @masonry-grid/vanillaor use a CDN:
import { BalancedMasonryGrid } from 'https://cdn.skypack.dev/@masonry-grid/vanilla' -
Now you can import constructor and start using it in your project:
<div class="masonry"><!-- Each item must have aspect-ratio set by --width and --height CSS variables --><div class="frame" style="--width: 4; --height: 3;"><img src="https://picsum.photos/400/300" /></div><div class="frame" style="--width: 1; --height: 1;"><img src="https://picsum.photos/200/200" /></div><div class="frame" style="--width: 3; --height: 4;"><img src="https://picsum.photos/300/400" /></div><div class="frame" style="--width: 3; --height: 4;"><img src="https://picsum.photos/300/400" /></div><div class="frame" style="--width: 1; --height: 1;"><img src="https://picsum.photos/200/200" /></div><div class="frame" style="--width: 4; --height: 3;"><img src="https://picsum.photos/400/300" /></div></div>.masonry {/* Required styles */display: grid;overflow: hidden;/* Optional styles */gap: 10px;grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));}.frame {/* Required styles */aspect-ratio: var(--width) / var(--height);}import { BalancedMasonryGrid } from '@masonry-grid/vanilla'new BalancedMasonryGrid(document.querySelector('.masonry')) -
Run your application and see the masonry grid in action!
-
Install package by running the following command in your terminal:
pnpm add @masonry-grid/reactyarn add @masonry-grid/reactnpm install @masonry-grid/react -
Now you can import the components and start using them in your React project:
import { BalancedMasonryGrid, Frame } from '@masonry-grid/react'function Gallery() {return (<BalancedMasonryGridframeWidth={200}gap={10}><Frame width={4} height={3}><img src='https://picsum.photos/400/300' alt='Photo 1' /></Frame><Frame width={1} height={1}><img src='https://picsum.photos/200/200' alt='Photo 2' /></Frame><Frame width={3} height={4}><img src='https://picsum.photos/300/400' alt='Photo 3' /></Frame><Frame width={3} height={4}><img src='https://picsum.photos/300/400' alt='Photo 4' /></Frame><Frame width={1} height={1}><img src='https://picsum.photos/200/200' alt='Photo 5' /></Frame><Frame width={4} height={3}><img src='https://picsum.photos/400/300' alt='Photo 6' /></Frame></BalancedMasonryGrid>)} -
Run your React application and see the masonry grid in action!
-
Install package by running the following command in your terminal:
pnpm add @masonry-grid/svelteyarn add @masonry-grid/sveltenpm install @masonry-grid/svelte -
Now you can import the components and start using them in your Svelte project:
<script>import { BalancedMasonryGrid, Frame } from '@masonry-grid/svelte'</script><BalancedMasonryGridframeWidth={200}gap={10}><Frame width={4} height={3}><img src='https://picsum.photos/400/300' alt='Photo 1' /></Frame><Frame width={1} height={1}><img src='https://picsum.photos/200/200' alt='Photo 2' /></Frame><Frame width={3} height={4}><img src='https://picsum.photos/300/400' alt='Photo 3' /></Frame><Frame width={3} height={4}><img src='https://picsum.photos/300/400' alt='Photo 4' /></Frame><Frame width={1} height={1}><img src='https://picsum.photos/200/200' alt='Photo 5' /></Frame><Frame width={4} height={3}><img src='https://picsum.photos/400/300' alt='Photo 6' /></Frame></BalancedMasonryGrid> -
Run your Svelte application and see the masonry grid in action!