CHAPTER 34

CSS Image Gallery

Learn how CSS Grid and other CSS properties can be used to create responsive image galleries.

01. What is CSS Image Gallery?

An image gallery displays multiple images in an organized layout.
CSS Grid is useful for creating responsive gallery layouts.
Images can be styled using width, height and object-fit.

02. Syntax

.gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 15px; } .gallery img { width: 100%; height: 200px; object-fit: cover; }

03. Example

CSS / HTML Example AIFA Coding Lab
<!DOCTYPE html>
<html>
<head>

<style>

.gallery {

    display: grid;

    grid-template-columns:
        repeat(3, 1fr);

    gap: 15px;

}

.gallery img {

    width: 100%;

    height: 180px;

    object-fit: cover;

    border-radius: 12px;

}

@media(max-width:700px) {

    .gallery {

        grid-template-columns: 1fr;

    }

}

</style>

</head>

<body>

<div class="gallery">

    <img src="https://via.placeholder.com/400x300"
         alt="Gallery image">

    <img src="https://via.placeholder.com/400x300"
         alt="Gallery image">

    <img src="https://via.placeholder.com/400x300"
         alt="Gallery image">

</div>

</body>
</html>

04. Important Points

✓ Use width: 100% for responsive images.
✓ object-fit: cover keeps images visually consistent.
✓ CSS Grid is useful for gallery layouts.
✓ Use media queries for smaller screens.

05. Common Mistakes

⚠ Using very large images without optimization.
⚠ Forgetting alt text.
⚠ Creating galleries that overflow on mobile.
⚠ Using fixed dimensions without considering different image ratios.

06. Real World Use

Image galleries are used for portfolios, photography websites, product pages, school websites and project showcases.

Ready to Practice? 🚀

Open the AIFA CSS Editor and practice this chapter live.

Try It Yourself →
Practice Exercise

Create a Responsive Gallery

Create a three-column image gallery on desktop and change it to one column on small screens.