CSS Image Gallery
An image gallery is a great exercise because it combines many CSS skills: layout, sizing, borders, shadows, hover effects and responsive behaviour. In this lesson the "images" are gradient and SVG tiles, so every example works offline — swap in real <img> tags in your own projects.
The structure
Each gallery item is usually a <figure> containing the image and a <figcaption>:
<div class="gallery">
<figure>
<img src="lake.jpg" alt="A calm lake at dawn">
<figcaption>Lake at dawn</figcaption>
</figure>
…
</div>A responsive grid gallery
CSS Grid with repeat(auto-fill, minmax(…)) creates as many columns as fit, and they reflow automatically when the screen changes width. Resize the playground to watch it.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; margin: 10px; }
.gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)); gap: 10px; }
figure { margin: 0; background: white; border: 1px solid #ddd; border-radius: 8px; overflow: hidden; box-shadow: 0 2px 4px rgba(0,0,0,.08); }
.img { aspect-ratio: 4 / 3; }
figcaption { padding: 6px 8px; font-size: 13px; color: #555; }
.g1 { background: linear-gradient(#ffcc80, #ef6c00); }
.g2 { background: linear-gradient(#80deea, #00838f); }
.g3 { background: linear-gradient(#c5e1a5, #558b2f); }
.g4 { background: linear-gradient(#f48fb1, #ad1457); }
.g5 { background: linear-gradient(#b39ddb, #4527a0); }
.g6 { background: linear-gradient(#fff59d, #f9a825); }
</style>
</head>
<body>
<div class="gallery">
<figure><div class="img g1"></div><figcaption>Desert</figcaption></figure>
<figure><div class="img g2"></div><figcaption>Lagoon</figcaption></figure>
<figure><div class="img g3"></div><figcaption>Forest</figcaption></figure>
<figure><div class="img g4"></div><figcaption>Blossom</figcaption></figure>
<figure><div class="img g5"></div><figcaption>Dusk</figcaption></figure>
<figure><div class="img g6"></div><figcaption>Field</figcaption></figure>
</div>
</body>
</html>| Technique | Purpose |
|---|---|
grid-template-columns: repeat(auto-fill, minmax(110px, 1fr)) |
Responsive columns, at least 110px wide |
gap |
Even spacing between tiles |
aspect-ratio: 4 / 3 |
Every tile has the same shape |
overflow: hidden + border-radius |
Rounds the image corners |
object-fit: cover (for <img>) |
Fills the tile without distortion, cropping if needed |
Tip: With real images, use
img { width: 100%; aspect-ratio: 4/3; object-fit: cover; display: block; }so mismatched photo sizes still produce a tidy grid.
Using real img elements
Here the images are SVGs embedded as data URIs, styled exactly as photos would be.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; margin: 10px; }
.gallery { display: flex; flex-wrap: wrap; gap: 10px; }
.gallery img { width: 120px; height: 90px; object-fit: cover; display: block; border-radius: 8px; border: 3px solid white; box-shadow: 0 3px 8px rgba(0,0,0,.25); }
</style>
</head>
<body>
<div class="gallery">
<img alt="mountain" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 160 90'%3E%3Crect width='160' height='90' fill='%2390caf9'/%3E%3Cpath d='M0 90 L50 30 L80 60 L110 20 L160 90Z' fill='%23455a64'/%3E%3C/svg%3E">
<img alt="sun" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 90 160'%3E%3Crect width='90' height='160' fill='%23ffe0b2'/%3E%3Ccircle cx='45' cy='60' r='30' fill='%23ff9800'/%3E%3C/svg%3E">
<img alt="sea" src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 120 120'%3E%3Crect width='120' height='120' fill='%23b2ebf2'/%3E%3Crect y='70' width='120' height='50' fill='%230097a7'/%3E%3C/svg%3E">
</div>
<p>The SVGs have different shapes, but <code>object-fit: cover</code> makes them uniform.</p>
</body>
</html>Hover effects
Galleries feel alive with subtle hover effects: zoom the image inside its frame, lift the card, or reveal a caption.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; margin: 10px; }
.gallery { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.item { position: relative; overflow: hidden; border-radius: 10px; aspect-ratio: 1; transition: transform .25s, box-shadow .25s; }
.item .img { position: absolute; inset: 0; transition: transform .4s; }
.item:hover { transform: translateY(-4px); box-shadow: 0 10px 18px rgba(0,0,0,.25); }
.item:hover .img { transform: scale(1.15); }
.cap { position: absolute; left: 0; right: 0; bottom: 0; padding: 6px; color: white; background: rgb(0 0 0 / .55); font-size: 13px; opacity: 0; transition: opacity .3s; }
.item:hover .cap { opacity: 1; }
.a { background: radial-gradient(circle at 30% 30%, #fff176, #f57f17); }
.b { background: radial-gradient(circle at 70% 40%, #80cbc4, #00695c); }
.c { background: radial-gradient(circle at 50% 80%, #ef9a9a, #b71c1c); }
</style>
</head>
<body>
<div class="gallery">
<div class="item"><div class="img a"></div><div class="cap">Sunrise</div></div>
<div class="item"><div class="img b"></div><div class="cap">Reef</div></div>
<div class="item"><div class="img c"></div><div class="cap">Ember</div></div>
</div>
</body>
</html>A featured-image layout
Grid lets one item span more cells for a magazine look.
<!DOCTYPE html>
<html>
<head>
<style>
body { margin: 10px; }
.mosaic { display: grid; grid-template-columns: repeat(4, 1fr); grid-auto-rows: 70px; gap: 6px; }
.mosaic div { border-radius: 6px; }
.big { grid-column: span 2; grid-row: span 2; background: linear-gradient(135deg, #7b1fa2, #e040fb); }
.wide { grid-column: span 2; background: linear-gradient(135deg, #0277bd, #4fc3f7); }
.s1 { background: #ffb74d; } .s2 { background: #aed581; } .s3 { background: #4db6ac; } .s4 { background: #f06292; }
</style>
</head>
<body>
<div class="mosaic">
<div class="big"></div><div class="s1"></div><div class="s2"></div>
<div class="wide"></div><div class="s3"></div><div class="s4"></div>
</div>
</body>
</html>Always give real images a meaningful alt text describing the picture for screen-reader users.
Test yourself
What does repeat(auto-fill, minmax(120px, 1fr)) do?
- Always creates 120 columns
- Creates as many columns of at least 120px as fit, sharing leftover space
- Makes every image 120px tall
- Repeats one image
Which property makes differently sized photos fill equal tiles without distortion?
- object-fit: cover
- background-size: auto
- image-rendering: crisp
- float: left
To zoom an image on hover without it spilling out of its frame, the frame needs…
- z-index: 1
- overflow: hidden
- display: inline
- position: static