Skip to main content

Command Palette

Search for a command to run...

CSS Easy Masonry Grid

Published
0 min read
CSS Easy Masonry Grid
C

I'm a full-stack developer from South Africa 🇿🇦. I love writing about JavaScript, HTML and CSS.

Today we are going to look into making a straightforward CSS Masonry grid. For those who don't know what a Masonry grid is, it's a grid where items that are not equal in size match up. Back in the days, this was quite hard to achieve, and you had to use some bloated JavaScript framework to accomplish this. Nowadays, we can make it with very little use of CSS.

HTML Structure

<div class="masonry">
  <img
    src="https://images.unsplash.com/photo-1551675705-72513c2722a2?ixlib=rb-1.2.1&ixid=eyJhcHBfaWQiOjEyMDd9&auto=format&fit=crop&w=800&q=60"
  />
  ... More images
</div>

As you can see, I tried to keep it as simple as possible by only using one masonry container and adding several images in it.

CSS Masonry

.masonry {
  column-count: 3;
  column-gap: 0;
}
.masonry img {
  display: block;
  max-width: 100%;
}

Yes, that's all! We define a column-count which will make 3 columns and I added a gap of 0 to make it look better. Then each image we give a max-width so it doesn't break out.

View and play with this demo on Codepen.

View on Codepen.

Browser Support

The browser support for column-count is actually impressive! IE10+, and all major browsers.

View on caniuse

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter

M

The problem with these masonry scripts has always been they work great for images but once you try to use it with a div and or tables of data, it completely fails unless theres a fixed width container / children (ie: not responsive)

I'd love to see a tutorial on that - I've used dozens of masonry scripts and they never work right for div children

1
C

Hey Mario,

Your absolutely right, let me put it on my backlog and see what I can do. Is there a specific content you are looking to see as a demo or just a random news site?

M

Daily Dev Tips just a generic everyday 10+ divs on a page with title, sub title and body content of varying length. The content shouldn't matter though. What matters is variable length data (text, table data, whatever) in the div and stack properly.

More from this blog

D

Daily Dev Tips

887 posts

Looking to get into development? As a full-stack developer I guide you on this journey and give you bite sized tips every single day 👊