3 Easy CSS Only Masonry Layouts

3 Easy CSS Only Masonry Layouts

Feed full of gorgeous images of different sizes, arranging perfectly like bricks in a wall. That’s masonry layout. And it requires JavaScript. Or… does it? In this article I’m going to show you how you can achieve that look with just CSS. Feed full of gorgeous images of different sizes, arranging perfectly like bricks in a wall. That’s masonry layout.

Feed full of gorgeous images of different sizes, arranging perfectly like bricks in a wall. That’s masonry layout. And it requires JavaScript. Or… does it?

In this article I’m going to show you how you can achieve that look with just CSS.

Preparing the elements

We need bricks to build the wall. Since hardcoding 30 divs isn’t necessarily the most exciting thing to do, we’re just going to use a for loop. Don’t worry though, the styling itself will be done purely with CSS. Let’s use the lorem picsum API to get some nice images from Unsplash as backgrounds for the divs.

for (let i = 0; i <= 30; i++) {
  const div = document.createElement('div');
  div.style.backgroundImage = `url(https://picsum.photos/500/500?random=${i})`;
  document.body.appendChild(div);
}

Set the height and width so we can actually see the images, add some margin, border radius and adjust the background position and size:

div {
  border-radius: 10px;
  background-size: cover;
  background-position: center;
  margin: 0.3em;
  height: 10em;
  width: 10em;
}

Image for post

Photos from Unsplash

Sweet! Now let’s get to the layouts.

css-grid web-development flexbox css

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

Hire CSS Developer

Want to develop a website or re-design using CSS Development? We build a website and we implemented CSS successfully if you are planning to **[Hire CSS Developer](https://hourlydeveloper.io/hire-dedicated-css-developer/ "Hire CSS Developer")**...

Flexbox and CSS Grid - "The Dream Team"

If right run a search for "CSS Grid and Flexbox," you'll find mostly articles or videos with the title "CSS Grid VS Flexbox". These articles and videos assume one is better than the other. However, the truth is that both are great and one isn't better than the other. In this article, I'm gonna show you what both are capable of, things that make them different, and why you should use them together to make your web page look amazing.

Why Web Development is Important for your Business

With the rapid development in technology, the old ways to do business have changed completely. A lot more advanced and developed ways are ...

Important Reasons to Hire a Professional Web Development Company

    You name the business and I will tell you how web development can help you promote your business. If it is a startup or you seeking some...

CSS Flexbox vs. CSS Grid

CSS Flexbox vs. CSS Grid - The basic difference between CSS Grid Layout and CSS Flexbox Layout is that flexbox was designed for layout in one dimension - either a row or a column. Grid was designed for two-dimensional layout - rows, and columns at the same time.