React Responsive Masonry

2.7.2 · active · verified Sun Apr 19

react-responsive-masonry is a lightweight React component library for creating responsive masonry layouts using CSS Flexbox. It provides two main components: `Masonry` for a static column count and `ResponsiveMasonry` for dynamically adjusting columns and gutters based on breakpoints. The current stable version is 2.7.2. Releases appear to be driven by bug fixes and minor feature enhancements rather than a strict cadence, with the last major update to v2.0.0 in 2020 introducing ES6 modules. Its key differentiator is its simplicity and reliance on flexbox, avoiding complex JavaScript layout algorithms for performance and ease of use, making it suitable for image galleries or card layouts where item order isn't strictly sequential.

Common errors

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to set up a responsive masonry layout using both `ResponsiveMasonry` and `Masonry` components, showing how columns and gutters adapt to different screen sizes.

import React from 'react';
import Masonry, { ResponsiveMasonry } from 'react-responsive-masonry';

// Imagine these are your child components or elements
const ChildA = () => <div style={{ height: '100px', background: '#e0e0e0', margin: '5px' }}>Item A</div>;
const ChildB = () => <div style={{ height: '150px', background: '#d0d0d0', margin: '5px' }}>Item B</div>;
const ChildC = () => <div style={{ height: '80px', background: '#c0c0c0', margin: '5px' }}>Item C</div>;
const ChildD = () => <div style={{ height: '200px', background: '#b0b0b0', margin: '5px' }}>Item D</div>;
const ChildE = () => <div style={{ height: '120px', background: '#a0a0a0', margin: '5px' }}>Item E</div>;
const ChildF = () => <div style={{ height: '90px', background: '#909090', margin: '5px' }}>Item F</div>;

function App() {
  return (
    <div style={{ padding: '20px', maxWidth: '1200px', margin: '0 auto' }}>
      <h1>Responsive Masonry Example</h1>
      <ResponsiveMasonry
        columnsCountBreakPoints={{ 350: 1, 750: 2, 900: 3, 1200: 4 }}
        gutterBreakPoints={{ 350: "10px", 750: "15px", 900: "20px", 1200: "25px" }}
      >
        <Masonry gutter="10px">
          <ChildA />
          <ChildB />
          <ChildC />
          <ChildD />
          <ChildE />
          <ChildF />
          {/* More children here */}
        </Masonry>
      </ResponsiveMasonry>
    </div>
  );
}

export default App;

view raw JSON →