DEV Community

Cover image for CSS: masonry layout
Reuven Hozias
Reuven Hozias

Posted on

5 1

CSS: masonry layout

What is masonry layout?

from MDN:

Masonry layout is a layout method where one axis uses a typical strict grid layout, most often columns, and the other a masonry layout. On the masonry axis, rather than sticking to a strict grid with gaps being left after shorter items, the items in the following row rise up to completely fill the gaps.

pinterest.com's layout is one classic example of it:

Image description

What can we use from our CSS toolbox?

grid

We use a simple HTML markup:

<div class="photos">
  <img src="./img/image-1.jpg" alt="sample">
  ...
  ...
</div>
Enter fullscreen mode Exit fullscreen mode

My first shot was grid & grid-template-column

.photos {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
  gap: 1rem;

  img{
    width: 100%;
  }
}
Enter fullscreen mode Exit fullscreen mode

Image description

The responsiveness is great, but we have gaps below each image.

column-count

next, use the column-count CSS container property.

The column-count CSS property breaks an element's content into the specified number of columns.

.photos {
  column-count: 4;

  img{
    width: 100%;
    margin-bottom: 1rem;
  }
}
Enter fullscreen mode Exit fullscreen mode

Image description

Not good.
The current layout looks as desired, but the images are scaled and not responsive. While we could use media queries to control responsiveness, we’re aiming for a more robust solution.

Using columns

The columns CSS shorthand property sets the number of columns to use when drawing an element's contents, as well as those columns' widths.

.photos {
  columns: 250px;

  img{
    width: 100%;
    margin-bottom: 1rem;
  }
}
Enter fullscreen mode Exit fullscreen mode

Image description

A single line of code. Amazing!

How does this work?

Each column is given a minimum width of 250px. If there is extra space beyond 250px, the columns will expand to fill the space. If the space is reduced, the number of columns will decrease accordingly.

Extras

We can limit the number of columns by setting the layout to a maximum of X columns:

.photos {
  columns: 250px 2;
  ...
  ...
}
Enter fullscreen mode Exit fullscreen mode

Image description

Using

columns
is not limited to masonry image layouts alone. We can also use it to style text columns: same CSS, different content.

Image description

Final thoughts

Was this helpful?
What was your use case?

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Cloudinary image

Optimize, customize, deliver, manage and analyze your images.

Remove background in all your web images at the same time, use outpainting to expand images with matching content, remove objects via open-set object detection and fill, recolor, crop, resize... Discover these and hundreds more ways to manage your web images and videos on a scale.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay