Skip to content

CSS Grid Photo Gallery

Easy ~30 min

What you'll build

A gallery that automatically arranges photos into as many columns as fit, with a click-to-zoom lightbox overlay.

Goals

  • Use CSS Grid with repeat(auto-fit, minmax()) for fluid columns
  • Create a lightbox overlay with CSS transitions
  • Handle click events to open and close the lightbox
  • Make the layout responsive without media queries

Prerequisites

  • HTML structure
  • CSS Grid basics
  • JavaScript event listeners

Steps

  1. 1

    Build the gallery HTML

    Create a .gallery container with several .gallery-item elements, each containing an img with a thumbnail and a data-full attribute pointing to the full-size image.

    Code
    <div class="gallery">
      <img class="gallery-item" src="thumb1.jpg" data-full="full1.jpg" alt="Photo 1">
      <img class="gallery-item" src="thumb2.jpg" data-full="full2.jpg" alt="Photo 2">
      <img class="gallery-item" src="thumb3.jpg" data-full="full3.jpg" alt="Photo 3">
      <img class="gallery-item" src="thumb4.jpg" data-full="full4.jpg" alt="Photo 4">
    </div>

    Expected result

    Four images render in a row (or stacked, before CSS is added).

  2. 2

    Apply CSS Grid auto-fit

    Use grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)) so the grid creates as many 200px columns as fit, expanding them to fill space. This is responsive without any media query.

    Code
    .gallery {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
      gap: 16px;
      padding: 16px;
    }
    .gallery-item {
      width: 100%;
      height: 200px;
      object-fit: cover;
      border-radius: 8px;
      cursor: pointer;
      transition: transform 0.2s;
    }
    .gallery-item:hover { transform: scale(1.03); }

    Expected result

    Images arrange into a fluid grid; resize the window and columns reflow automatically.

  3. 3

    Build the lightbox overlay

    Add a hidden .lightbox div with an img inside. Style it as a fixed full-screen overlay with a dark background, hidden by default and shown via .active.

    Code
    <div class="lightbox" id="lightbox">
      <img id="lightbox-img" src="" alt="">
    </div>
    
    <style>
    .lightbox {
      position: fixed; inset: 0;
      background: rgba(0,0,0,0.9);
      display: none;
      align-items: center; justify-content: center;
      z-index: 1000;
    }
    .lightbox.active { display: flex; }
    .lightbox img { max-width: 90%; max-height: 90%; }
    </style>

    Expected result

    The lightbox div exists but is invisible until .active is added.

  4. 4

    Wire the click handlers

    When a gallery item is clicked, set the lightbox img src to the data-full value and add .active. Clicking the lightbox itself closes it.

    Code
    const lightbox = document.getElementById('lightbox');
    const lightboxImg = document.getElementById('lightbox-img');
    
    document.querySelectorAll('.gallery-item').forEach(item => {
      item.addEventListener('click', () => {
        lightboxImg.src = item.dataset.full;
        lightbox.classList.add('active');
      });
    });
    lightbox.addEventListener('click', () => {
      lightbox.classList.remove('active');
    });

    Expected result

    Click a thumbnail → full image opens in overlay; click overlay → closes.

  5. 5

    Add keyboard support

    Add an Escape key listener to close the lightbox. This is a basic accessibility requirement for modal-like overlays.

    Code
    document.addEventListener('keydown', (e) => {
      if (e.key === 'Escape') {
        lightbox.classList.remove('active');
      }
    });

    Expected result

    Pressing Escape closes the lightbox when it is open.

Try it yourself

What you learned

You used CSS Grid's auto-fit to build a layout that is inherently responsive — no media queries needed — and added a lightbox with keyboard support.

Related Resources

Related Lessons

  • CSS Layout

    CSS Grid with auto-fit creates fluid responsive layouts.

  • HTML Structure

    Semantic containers for the gallery and lightbox.

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors

← Back to topic