Madras Academy
Home / Blog / html / How to Create a Masonry Grid Image Gallery with HTML & CSS
html

How to Create a Masonry Grid Image Gallery with HTML & CSS

March 12, 2025 admin 3 min read

Are you looking to build a stunning Masonry Grid Image Gallery using just HTML & CSS? ๐Ÿ“ธโœจ In this tutorial, weโ€™ll guide you through creating a fully responsive, stylish, and easy-to-implement image gallery that beautifully arranges images in a masonry layoutโ€”no JavaScript needed!


๐Ÿš€ Why Choose a Masonry Grid Layout?

A masonry grid is perfect for showcasing images, whether you’re building a portfolio, photography website, or an e-commerce store. Unlike traditional grids, masonry layouts dynamically arrange images in a visually appealing way, making use of available space efficiently.

โœ… Features of This Image Gallery:

โœ” Fully Responsive Design โ€“ Works on all devices ๐Ÿ“ฑ๐Ÿ’ป
โœ” Uses CSS Grid & Flexbox โ€“ No JavaScript required
โœ” Perfect for Portfolios & Photography Websites ๐Ÿ“ท
โœ” Easy to Customize & Implement ๐ŸŽจ


๐Ÿ”ฅ Step-by-Step Guide to Creating a Masonry Grid Gallery

1๏ธโƒฃ Setting Up the HTML Structure

Start by creating a simple HTML file and adding a container for your images:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Masonry Grid Gallery</title>
    <link rel="stylesheet" href="styles.css">
</head>
<body>
    <div class="gallery">
        <img src="1.jpg" alt="Image 1">
        <img src="2.jpg" alt="Image 2">
        <img src="3.jpg" alt="Image 3">
        <img src="4.jpg" alt="Image 4">
        <img src="5.jpg" alt="Image 5">
        <img src="6.jpg" alt="Image 6">
    </div>
</body>
</html>

2๏ธโƒฃ Styling the Gallery with CSS Grid

Now, letโ€™s add CSS to create the masonry effect:

body {
    font-family: Arial, sans-serif;
    background-color: #f5f5f5;
    display: flex;
    justify-content: center;
    align-items: center;
    min-height: 100vh;
    margin: 0;
}

.gallery {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(150px, 1fr));
    grid-auto-rows: 10px;
    gap: 10px;
    max-width: 800px;
    padding: 10px;
}

.gallery img {
    width: 100%;
    border-radius: 5px;
    object-fit: cover;
    grid-row: span 20;
    transition: transform 0.3s ease-in-out;
}

.gallery img:hover {
    transform: scale(1.05);
}

๐ŸŽฏ How It Works

  • CSS Grid dynamically arranges images in columns and rows.
  • grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); ensures images fit different screen sizes.
  • grid-auto-rows: 10px; creates the masonry effect by making row heights flexible.
  • grid-row: span 20; ensures images span different rows based on content.
  • Hover effect adds a slight zoom when you hover over an image.

๐ŸŒŸ Final Thoughts

With just HTML & CSS, youโ€™ve built a beautiful, responsive masonry grid gallery! ๐ŸŽ‰ This layout is perfect for showcasing portfolios, blogs, and photography collections without relying on JavaScript.

Want to learn more? Check out our video tutorial:
๐ŸŽฅ Watch the Full Guide on YouTube:

๐Ÿ’ป Get the Source Code on GitHub:
๐Ÿ”— Madras Academy GitHub Repo

๐Ÿ”ฅ Follow for More Web Development Tutorials!
๐Ÿ“Œ Donโ€™t forget to LIKE ๐Ÿ‘, SHARE ๐Ÿ”„ & SUBSCRIBE ๐Ÿ”” for more awesome web design content.

Happy Coding! ๐Ÿš€๐Ÿ’ป

RECOMMENDED COURSES

Continue learning with Madras Academy.

Here are a few courses selected from our current catalog.

Browse All Courses
AI+
Flagship

AI Full Stack Engineer

Build modern full-stack products that combine web engineering, APIs, databases and production-ready AI features.

18 Weeks Advanced
Next.jsTypeScriptFastAPI
W WhatsApp