HTML5 and CSS3 Lab 2: Build an Animated Responsive Photo Gallery with Grid, Transforms, Transitions and Keyframes
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
figureandfigcaptionconnect a picture and its captiongrid-template-columns: repeat(auto-fit, minmax(12rem, 1fr))makes the column count follow the width.widespans two columns on larger screens and becomes normal on phonesaspect-ratiokeeps tile shape steadyoverflow: hiddenon the figure keeps the zoomed picture inside the rounded cornersanimation-delaywithnth-childstaggers the entrancebothfill mode keeps tiles hidden until their delay ends
How We Tested It
| Window width | Sideways scroll | Grid columns |
|---|---|---|
| 390 px | None | 1 |
| 700 px | None | 3 |
| 1200 px | None | 4 |
- 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
- Add a tall tile that spans two rows
- Open a larger picture in a
dialogwhen a tile is clicked - 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.

