On this page7 sections
  1. Learning Objectives
  2. 1. A Responsive Gallery
  3. 2. Image Sprites
  4. Common Beginner Mistakes
  5. Practice Questions
  6. Practice Answers
  7. Key Takeaways

Lecture 023: Image Gallery & Image Sprites

Category5: Pseudo-classes & Components
Lecture023 of 057
Estimated video length10 min
TargetCSS3 · any modern browser
LevelBeginner

Two classic image techniques. A gallery lays out thumbnails neatly, with captions and hover effects. A sprite is one image file holding many small pictures, where CSS shows just one part at a time.


Learning Objectives

  • Build a responsive image gallery with Grid
  • Add captions and hover effects
  • Show part of a single image with background-position (sprites)

Example: drag the preview corner to see the columns change:

html
<style>
  .gallery { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 12px; }
  .gallery figure { margin: 0; border: 1px solid #dee2e6; border-radius: 10px; overflow: hidden; background: white; }
  .gallery img { display: block; width: 100%; height: 110px; object-fit: cover; transition: transform 0.3s; }
  .gallery figure:hover img { transform: scale(1.08); }
  .gallery figcaption { padding: 8px 10px; font: 13px system-ui, sans-serif; color: #495057; }
</style>

<div class="gallery">
  <figure><img src="https://picsum.photos/id/1015/300/220" alt="River valley"><figcaption>River valley</figcaption></figure>
  <figure><img src="https://picsum.photos/id/1016/300/220" alt="Desert canyon"><figcaption>Desert canyon</figcaption></figure>
  <figure><img src="https://picsum.photos/id/1018/300/220" alt="Mountain lake"><figcaption>Mountain lake</figcaption></figure>
  <figure><img src="https://picsum.photos/id/1019/300/220" alt="Sunset over the sea"><figcaption>Sunset over the sea</figcaption></figure>
  <figure><img src="https://picsum.photos/id/1036/300/220" alt="Snowy forest"><figcaption>Snowy forest</figcaption></figure>
  <figure><img src="https://picsum.photos/id/1039/300/220" alt="Waterfall"><figcaption>Waterfall</figcaption></figure>
</div>
  • repeat(auto-fill, minmax(140px, 1fr)) fits as many columns as there's room for.
  • object-fit: cover crops every thumbnail to the same size without stretching it.
  • overflow: hidden on the figure keeps the zoomed image inside the rounded corners.

2. Image Sprites

Instead of 4 small icon files (4 downloads), use one image with all 4 icons in a row, and show the right part with background-position.

Example: one 200×50 SVG holds four 50×50 icons:

html
<style>
  .icon {
    display: inline-block;
    width: 50px; height: 50px;
    background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='200' height='50'%3E%3Ccircle cx='25' cy='25' r='18' fill='%23e03131'/%3E%3Crect x='57' y='7' width='36' height='36' fill='%232f9e44'/%3E%3Cpolygon points='125,7 143,43 107,43' fill='%231971c2'/%3E%3Cpolygon points='175,6 181,20 196,20 184,29 188,44 175,35 162,44 166,29 154,20 169,20' fill='%23f59f00'/%3E%3C/svg%3E");
    background-repeat: no-repeat;
    border: 1px solid #dee2e6;
    margin: 4px;
  }
  .circle   { background-position: 0 0; }
  .square   { background-position: -50px 0; }
  .triangle { background-position: -100px 0; }
  .star     { background-position: -150px 0; }
  .star:hover { background-position: -150px 0; transform: scale(1.2); }
</style>

<span class="icon circle"></span>
<span class="icon square"></span>
<span class="icon triangle"></span>
<span class="icon star"></span>

Each icon is a 50×50 "window"; the negative background-position slides the big image along so the right picture shows through.

With HTTP/2 and SVG icons, sprites matter less than they used to, but you'll still find them in many existing sites and games.


Common Beginner Mistakes

1. Thumbnails of different heights. Give images a fixed height and object-fit: cover.

2. Huge images as thumbnails. Make small versions of the files for the gallery.

3. Sprite positions in the wrong direction. To show something further right in the image, use a negative x value.


Practice Questions

  1. Which Grid value makes a gallery fit as many columns as possible?
  2. How do you crop thumbnails to the same size without stretching them?
  3. Why put overflow: hidden on the figure?
  4. What is an image sprite?
  5. A sprite has 40×40 icons in a row. What background-position shows the third icon?

Practice Answers

  1. grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  2. A fixed height plus object-fit: cover;
  3. So the zoomed image on hover stays inside the figure's rounded edges.
  4. One image containing many small pictures, with CSS showing one part at a time.
  5. background-position: -80px 0;

Key Takeaways

  • Grid + auto-fill + minmax gives a responsive gallery.
  • object-fit: cover keeps thumbnails even; figcaption adds captions.
  • Sprites = one image + background-position to choose the piece.