A masonry gallery is a visually appealing grid layout where images of different sizes are arranged in a structured yet flexible way. This type of gallery is often used in photography websites, blogs, and portfolios. In this guide, we’ll show you how to create a responsive masonry gallery using only HTML and CSS, without relying on JavaScript or external libraries.

1. Understanding Masonry Layout
What Is a Masonry Gallery?
A masonry gallery is different from a standard grid because it allows items to be placed dynamically, filling available spaces without maintaining a fixed row structure. This makes it perfect for displaying images of varying heights while keeping an organized look.
Why Use Masonry Layout?
- Creates a modern and stylish appearance
- Optimizes space and makes the gallery look balanced
- Works well for image-heavy websites
2. Setting Up the HTML Structure
To create the gallery, we need a simple HTML structure. Each image will be inside a <div> container, wrapped inside a parent <div> called gallery.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Masonry Gallery</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<div class="gallery">
<div class="item"><img src="image1.jpg" alt="Gallery Image 1"></div>
<div class="item"><img src="image2.jpg" alt="Gallery Image 2"></div>
<div class="item"><img src="image3.jpg" alt="Gallery Image 3"></div>
<div class="item"><img src="image4.jpg" alt="Gallery Image 4"></div>
<div class="item"><img src="image5.jpg" alt="Gallery Image 5"></div>
</div>
</body>
</html>
3. Styling the Masonry Gallery with CSS
Now that we have the HTML structure, we’ll use CSS Grid to create a masonry effect.
Basic Styling for the Gallery
First, we apply basic styles to make the images responsive and aligned correctly.
body {
font-family: Arial, sans-serif;
background-color: #f9f9f9;
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
padding: 20px;
}
.gallery {
display: grid;
grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
gap: 10px;
width: 90%;
max-width: 1200px;
}
.item img {
width: 100%;
height: auto;
border-radius: 5px;
}
Creating the Masonry Effect
To allow images to stack dynamically like a masonry layout, we use CSS columns instead of standard grid rows.
.gallery {
column-count: 3;
column-gap: 10px;
}
.item {
display: inline-block;
margin-bottom: 10px;
width: 100%;
}
Making the Gallery Responsive
To ensure the masonry gallery looks great on all devices, we can adjust the number of columns dynamically.
@media (max-width: 768px) {
.gallery {
column-count: 2;
}
}
@media (max-width: 480px) {
.gallery {
column-count: 1;
}
}
Masonry Gallery With CSS Grid
CSS Grid can create a masonry-style layout when you let items span a different number of rows. This method needs no JavaScript, but you must set the row height and the span of each item yourself.
.gallery {
display: grid;
grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
grid-auto-rows: 10px;
gap: 12px;
}
.gallery img {
width: 100%;
display: block;
border-radius: 6px;
}
.item-tall { grid-row: span 28; }
.item-medium { grid-row: span 20; }
.item-short { grid-row: span 14; } Each row is only ten pixels high, so an item that spans 28 rows is 280 pixels tall, plus the gaps. This approach is predictable, but it works best when you know the image sizes in advance.
Masonry Layout Options Compared
| Method | Pros | Cons |
| CSS columns | Very simple, no JavaScript | Items flow down each column, so the visual order can confuse keyboard and screen reader users |
| CSS Grid with row spans | Precise control and good browser support | Row spans must be set manually or by a script |
| Native CSS masonry | The ideal future solution | Browser support has been limited and changing, so check current compatibility |
| JavaScript library | Handles unknown heights automatically | Adds extra code and can cause layout shifts |
Make Your Masonry Gallery Fast and Accessible
- Lazy load images. Add
loading="lazy"to images below the first screen. - Set width and height. Provide the dimensions so that the browser reserves space and the page does not jump.
- Compress images. Use modern formats such as WebP and appropriate sizes.
- Add alt text. Describe each image for visitors who use screen readers.
- Test on mobile. Reduce the number of columns on narrow screens.
Add a Simple Hover Effect
.gallery img {
transition: transform 0.3s ease, box-shadow 0.3s ease;
}
.gallery img:hover {
transform: scale(1.03);
box-shadow: 0 6px 16px rgba(0, 0, 0, 0.25);
} A subtle effect gives feedback without distracting visitors. Avoid large movements, and respect users who prefer reduced motion by using the prefers-reduced-motion media query.
Frequently Asked Questions
Can I build a masonry gallery without JavaScript?
Yes. CSS columns and CSS Grid can create the effect. JavaScript is helpful only when image heights are unknown or the order must stay strictly left to right.
Common Problems With a Masonry Gallery
- Large gaps between images. The row height or the span values do not match the image sizes. Adjust them, or set an aspect ratio for each image.
- Images stretch. Use
object-fit: coverif you give images a fixed height, so that they keep their proportions. - The layout jumps while loading. Provide width and height attributes, so that the space is reserved.
- The gallery is hard to use on phones. Use one or two columns on small screens.
Remember that a gallery is part of your content, so test it with real photographs and captions. Check how it looks with portrait, landscape, and square images together, and confirm that it still works when the page is zoomed in or viewed with a keyboard.
If you want to refresh the basics, see our explanation of what CSS is.
4. Final Thoughts
By combining HTML, CSS Grid, and CSS columns, you can easily create a beautiful masonry gallery without JavaScript. This method ensures the gallery remains lightweight, fast, and responsive across different devices.
If this guide seems too complex or time-consuming, I can help! I offer professional web development services to create stunning, responsive websites tailored to your needs. Contact me today to get started!
