HTML5 and CSS3 Lab 2: Build an Animated Responsive Photo Gallery with Grid, Transforms, Transitions and Keyframes

HTML5 and CSS3 55 min min read Updated: Nov 18, 2026 Intermediate
HTML5 and CSS3 Lab 2: Build an Animated Responsive Photo Gallery with Grid, Transforms, Transitions and Keyframes
Intermediate Topic 2 of 3

Goal

Build a gallery where tiles fill the width, one tile is wider, tiles fade in one after another and zoom softly on hover. The lab uses color gradients as pictures so it needs no image files. Replace them with real photos using img and object-fit: cover later.

Step 1: gallery.html

html <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <title>Photo Gallery</title> <link rel="stylesheet" href="lab2.css"> </head> <body> <main> <h1>Travel Gallery</h1> <ul class="gallery"> <li class="wide"><figure><div class="ph c1" role="img" aria-label="Sunrise over hills"></div><figcaption>Sunrise</figcaption></figure></li> <li><figure><div class="ph c2" role="img" aria-label="Green forest"></div><figcaption>Forest</figcaption></figure></li> <li><figure><div class="ph c3" role="img" aria-label="Blue lake"></div><figcaption>Lake</figcaption></figure></li> <li><figure><div class="ph c4" role="img" aria-label="Desert dunes"></div><figcaption>Desert</figcaption></figure></li> <li><figure><div class="ph c5" role="img" aria-label="Snowy peak"></div><figcaption>Peak</figcaption></figure></li> </ul> </main> </body> </html>

Step 2: gallery.css

css *, *::before, *::after { box-sizing: border-box; } body { margin: 0; font-family: system-ui, sans-serif; background: #f3f6fb; color: #1b2433; } main { max-width: 64rem; margin-inline: auto; padding: 1.5rem 1rem; } .gallery { list-style: none; margin: 0; padding: 0; display: grid; gap: .75rem; grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)); } .gallery .wide { grid-column: span 2; } figure { margin: 0; overflow: hidden; border-radius: .75rem; background: #fff; box-shadow: 0 2px 8px rgba(0,0,0,.1); } .ph { aspect-ratio: 4 / 3; transition: transform .4s ease; } .wide .ph { aspect-ratio: 8 / 3; } figure:hover .ph, figure:focus-within .ph { transform: scale(1.06); } figcaption { padding: .6rem .9rem; font-weight: 600; } .c1 { background: linear-gradient(135deg, #ff9a56, #ffd36b); } .c2 { background: linear-gradient(135deg, #1e8449, #7bd389); } .c3 { background: linear-gradient(135deg, #1565c0, #7ec8ff); } .c4 { background: linear-gradient(135deg, #c98a3b, #f4d9a0); } .c5 { background: linear-gradient(135deg, #546e7a, #e3f2fd); } .gallery li { animation: rise .6s ease-out both; } .gallery li:nth-child(2) { animation-delay: .1s; } .gallery li:nth-child(3) { animation-delay: .2s; } .gallery li:nth-child(4) { animation-delay: .3s; } .gallery li:nth-child(5) { animation-delay: .4s; } @keyframes rise { from { opacity: 0; transform: translateY(16px); } to { opacity: 1; transform: none; } } @media (max-width: 600px) { .gallery .wide { grid-column: auto; } .wide .ph { aspect-ratio: 4 / 3; } } @media (prefers-reduced-motion: reduce) { .gallery li { animation: none; } .ph { transition: none; } }

How It Works

  • figure and figcaption connect a picture and its caption
  • grid-template-columns: repeat(auto-fit, minmax(12rem, 1fr)) makes the column count follow the width
  • .wide spans two columns on larger screens and becomes normal on phones
  • aspect-ratio keeps tile shape steady
  • overflow: hidden on the figure keeps the zoomed picture inside the rounded corners
  • animation-delay with nth-child staggers the entrance
  • both fill mode keeps tiles hidden until their delay ends

How We Tested It

Window widthSideways scrollGrid columns
390 pxNone1
700 pxNone3
1200 pxNone4
  • After pointing at a tile, the picture's transform was matrix(1.06, 0, 0, 1.06, 0, 0), which is a 6 percent zoom
Real photos Swap the div for img with alt, width and height. Keep object-fit: cover so different shaped photos fill their tiles.

Your Turn

  1. Add a tall tile that spans two rows
  2. Open a larger picture in a dialog when a tile is clicked
  3. Pause the animation on hover with animation-play-state

Conclusion

A responsive gallery combines figure markup, auto-fit grid, transforms, transitions and keyframes, with reduced motion respected.

Quick Summary: Goal; HTML; CSS; how it works; tests; exercises.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators