LogRocket Blog

Creating CSS masonry-style layouts

Read the original on blog.logrocket.com ↗

Editor’s note: This article was last updated by Rahul Chhodde to cover the latest CSS masonry developments, replacing grid-template-rows: masonry with display: grid-lanes. The update includes revised code examples, browser support details, interactive CodePen demos, CSS column fallbacks, and guidance on CLS and accessibility.

Creating Css Masonry Style Layouts

When designing a page with content overflowing the viewport, an indirect way to instruct users to keep scrolling is to display new content partially. One such approach is the masonry layout, which places elements inline and allows them to move up to fill empty spaces.

Within this tutorial, we will explore the experimental masonry layout added to the CSS Grid Level 3 Specification. We will also consider other similar ways to replicate the masonry layout with CSS.

Jump ahead:

Before you begin building, let’s take some time out to understand how the concept of masonry-style layouts came about.

Understanding the masonry layout

As its name suggests, the masonry layout has to do with the masonry craft in which workers called masons laid bricks to build concrete walls.

In the practice of their craft, masons often stacked bricks using the stretcher bond pattern, meaning the bricks were staggered by half over each other. Besides its stability and aesthetic appeal, the stretcher bond pattern also reduced material waste, as there was no need to cut the bricks in half at every joint.

When it comes to front-end development, the masonry layout has proven to be an effective way to display multiple items in a grid by partially revealing content. This layout has become widely adopted and popularized by platforms such as Pinterest.

With no native CSS masonry layout support, developers relied on using libraries such as Masonry.js or Brick.js to replicate this visual design. These libraries can accomplish masonry-style layouts without CSS by using JavaScript to manipulate the DOM, then placing each element within the appropriate spaces.

However, these libraries were reported to have a drastic impact on performance. They had to make frequent, expensive computations to find the available areas within the layout, resulting in a laggy scroll experience.

Fast forward to more recent years, now that CSS specifications have improved and become more robust, can developers achieve masonry layouts using only CSS? Let’s explore how to create a masonry layout in CSS by building the feed page of a sample blog website.

Creating a sample masonry CSS project

Let’s create a page to display the blog posts. Each post element will contain a cover image, title, and description as shown in the prototype below:

Prototype Of Page Styled Using Css Masonry Layout With Element Tiles Represented With Grey Boxes And Lines

To get started, create a directory to store the HTML and CSS files for the webpage about to be built, or start with a CodePen to test things instantly without a setup. 
Add the contents of the code block below to the markup. To make it easier to use, host the images remotely in a bucket on Cloudinary or use an image placeholder service like Picsum:
<div class="site-container">
  <div class="gallery-container">
    <figure class="gallery-item">
      <img
        src="..."
        alt="..."
        width="..."
        height="..."
      />
      <h2 class="gallery-item-title">...</h2>
    </figure>

    <figure class="gallery-item">
      <!-- Contents of another masonry tile -->
    </figure>
    <!-- More masonry tiles -->
  </div>
</div>

 

Make sure to provide the correct width and height attributes to your images to avoid cumulative layout shift (CLS) issues.
At this point, the webpage has some structure with no styles applied. Let’s proceed to add some styling to it in the next section.

Adding initial styles to our CSS masonry project

Start with CSS normalization: either use an existing CSS reset solution or employ a minimal reset as shown below:

*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
}

img {
  max-width: 100%;
  display: block;
  height: auto;
}

body {
  font: 1rem/1.6 sans-serif;
  background: #ccc;
}

/* Gallery styles */
.gallery-item { background: white }
.gallery-item img { width: 100% }

.gallery-item-title {
  padding: 1rem;
  text-align: center;
}

The gallery items appear like cards now, but the parent div or .gallery-container is still needs more work to achieve the masonry look. Here’s how it looks like at the moment:

Over the next subsections, you will explore two different ways to create a masonry look. Let’s start with the experimental CSS grid masonry approach.

Achieving a masonry layout with CSS grid-lanes display

Open the index.css file and add the .gallery-container selector to the existing selectors you added during the previous step:

:root {
  --masonry-cols: 3;
  --masonry-gap: 1rem;
}

.gallery-container {
   display: grid-lanes;
   grid-template-columns: repeat(var(--masonry-cols), 1fr);
   grid-gap: var(--masonry-gap);
}

We are using some CSS custom properties for better organization and easier customization. When adjusting the layout for different screen sizes, we can just override the --masonry-cols property at each breakpoint instead of rewriting the entire block.

Check out a working demo first before going any further. I’ve added a debug button to inspect the flow of masonry items, which will make sense as we move further in the article:

The magic word within the .gallery-container selector above is the grid-lanes value provided to the display property, which, if supported, connects to the CSS layout engine in the browser and triggers the masonry layout for the associated element.

CSS grid-lanes support

Until 2025, developers used grid-template-rows: masonry to test browser support for native masonry layouts, which the CSS Working Group later dropped in favour of this new display property called grid-lanes.

The support for the grid-lanes display is far better compared to its predecessor property. It has stable support on Safari 26.4+ now and is available as an experimental feature on other major browsers.

Even though it is expected to become a stable feature soon, grid-lanes won’t work unless the target browser has it enabled under the flags or supports it out of the box. The image below shows the layout being viewed with the grid-lanes display enabled (left) and disabled or not supported (right):

At this point, you have learned how to create a masonry layout using the experimental CSS grid-lanes display property. Considering that this property is yet to be released with full browser support, you cannot use it in production yet.

Let’s explore another masonry layout solution using CSS columns, which can be considered a fallback to grid-lanes in production builds.

Imitating a masonry layout using CSS columns

This traditional approach leverages the CSS multi-column layout with a defined column-count to arrange elements within columns without a strict height, thus creating a masonry layout effect.

Start by replacing the rules within the .gallery-container and figure class selectors with the code below:

:root {
  --masonry-cols: 3;
  --masonry-gap: 1rem;
}

.gallery-container {
  column-count: var(--masonry-cols);
  gap: var(--masonry-gap);
}

.gallery-item {
  break-inside: avoid;
  display: block;
  margin-bottom: var(--masonry-gap);
}

The new CSS rules above will style the blog posts into a three-column layout without a defined vertical axis. The column-count and column-gap properties in the gallery container handle the column layout, while the break-inside property in gallery items prevents them from splitting across columns.

This arrangement will cause the elements to fill up the available vertical space, making it appear visually similar to a masonry layout as shown below. Use the “Debug Masonry” button to expose the flow/order of the items:

As shown in the demo above, the ordering and sequence of the blog posts are vertical, from top to bottom,  rather than being shown horizontally from left to right.

Before adopting this approach in production, be aware of its only major trade-off: masonry items will flow vertically, which can be a UX problem for layouts where the horizontal visual order matters.

Without a visual hint to explain the ordering of elements, users will expect the elements to flow from left to right. Those who rely on their keyboards to navigate the list will find this very confusing, as each press of the Tab key will move them through the content from top to bottom, rather than right to left as visually expected.

CSS multi-column layout as a fallback to grid-lanes masonry

In cases where our grid-lanes masonry may lack support, we may add the above-discussed CSS columns masonry as a fallback. Instead of going stable-first, let’s keep our code as is and place CSS columns inside a negative @supports block to check for the lack of support for grid-lanes.

.gallery-container {
  display: grid-lanes;
  grid-template-columns: repeat(var(--masonry-cols), 1fr);
  gap: var(--masonry-gap);
}

@supports not (display: grid-lanes) {
  .gallery-container {
    display: block;
    columns: var(--masonry-cols);
    gap: var(--masonry-gap);
  }

  .gallery-item {
    break-inside: avoid;
    margin-bottom: var(--masonry-gap);
  }
}

This is easily the best performing masonry setup, provided you don’t need the grid to flow from left to right all the time. Here’s a working implementation featuring a CSS grid-lanes masonry with a column-count fallback:

Conclusion

Huge congratulations to you on completing this tutorial!

At the start of the tutorial, we set out to learn more about the masonry layout and how we could achieve it using only CSS. To achieve our goal, we dug deeper into the origin of the masonry layout and built the masonry grid using two different approaches with CSS.

As you anticipate the general release of the CSS grid-lanes, you can check if a user’s browser supports it through a @supports feature query. Within this rule, you can either replace your masonry hacks with the simplified implementation once it’s supported, or check for a lack of support and then add fallbacks just like we did.

If you enjoyed learning about the experimental masonry property for the grid layout, check out some articles about other new CSS features, such as LCH and LAB support.

The post Creating CSS masonry-style layouts appeared first on LogRocket Blog.