A Masonry component leveraging CSS and native React rendering, for fast, responsive masonry layouts
Existing solutions like React wrapped DeSandro Masonry, while popular, donât actually leverage Reactâs highly optimized Virtual DOM renderer and in DeSandro Masonryâs case, actually renders elements twice before showing the layout. All of this is ok but we found it to lead to a slow, âlaggyâ user experience that would occasionally miss-render our layout.
Our need for a simple Masonry layout that was fast, used Reactâs Virtual DOM without needing jQuery or other dependencies led us to explore what we could do with the latest techniques using just CSS within a React Component.
Between flexbox, css columns, css grid we settled on plain olâ divâs and a dab of flexbox that allows for âfluidâ responsive layouts by default but most importantly is true to Reacts rendering lifecycle.
react-masonry-css
Is a React Component with a simple interface to order items into the desired columns at specified breakpoints. With minimal CSS this leads to a quick, reliable solution that also has great browser support along with rendering performance.
Add react-masonry-css
to your project:
npm install react-masonry-css
In your React ComponentâŚ
import Masonry from 'react-masonry-css'
//...
<Masonry
breakpointCols={3}
className="my-masonry-grid"
columnClassName="my-masonry-grid_column">
{/* array of JSX items */}
</Masonry>
And, CSS:
.my-masonry-grid {
display: -webkit-box; /* Not needed if autoprefixing */
display: -ms-flexbox; /* Not needed if autoprefixing */
display: flex;
margin-left: -30px; /* gutter size offset */
width: auto;
}
.my-masonry-grid_column {
padding-left: 30px; /* gutter size */
background-clip: padding-box;
}
/* Style your items */
.my-masonry-grid_column > div { /* change div to reference your elements you put in <Masonry> */
background: grey;
margin-bottom: 30px;
}
Different columns can be specified by passing an object containing keyâs of the window widths and their value as the number of columns. To have a fallback value, use the default
key.
const breakpointColumnsObj = {
default: 4,
1100: 3,
700: 2,
500: 1
};
//...
<Masonry
breakpointCols={breakpointColumnsObj}
className="my-masonry-grid"
columnClassName="my-masonry-grid_column"
>
<div>My Element</div>
<div>My Element</div>
<div>My Element</div>
<div>My Element</div>
</Masonry>
breakpointCols=
optional (defaults to 2 columns)className
for the containercolumnClassName
class name added to each generated columnhttps://paulcollett.github.io/react-masonry-css/demo/
outputting an array of items:
var items = [
{id: 1, name: 'My First Item'},
{id: 2, name: 'Another item'},
{id: 3, name: 'Third Item'},
{id: 4, name: 'Here is the Fourth'},
{id: 5, name: 'High Five'}
];
// Convert array to JSX items
items = items.map(function(item) {
return <div key={item.id}>{item.name}</div>
});
<Masonry
breakpointCols={myBreakpointsAndCols}
className="my-masonry-grid"
columnClassName="my-masonry-grid_column"
>
{items}
</Masonry>
We can add the following to the above CSS to further adjust the layout between screen sizes.
/* Optional, different gutter size on mobile */
@media (max-width: 800px) {
.my-masonry-grid {
margin-left: -15px; /* gutter size offset */
}
.my-masonry-grid_column {
padding-left: 15px; /* gutter size offset */
}
.my-masonry-grid_column > div {
margin-bottom: 15px; /* space between items */
}
}
You can use react-masonry-css
with Preact when using preact/compat
Improve your frontend builds with dynamic placeholder images and dummy text from DummyJs.com. https://www.npmjs.com/package/dummyjs
https://github.com/paulcollett/react-masonry-css/issues/
Contact me direct: