Lesson 31 of 36
HTML Tutorial

Responsive Images and Figures

Serve the right image to each screen with srcset, sizes and picture, and caption images with figure.

3 min readBeginnerWeb FundamentalsHTML Media

A phone does not need the same giant photo as a 4K monitor. Responsive images let the browser pick the best file, saving data and speeding up pages. We will also meet figure for captions.

srcset and sizes#

Provide several widths of the same image and let the browser choose:

html
<img
  src="images/terrace-800.jpg"
  srcset="images/terrace-400.jpg 400w,
          images/terrace-800.jpg 800w,
          images/terrace-1600.jpg 1600w"
  sizes="(min-width: 900px) 50vw, 100vw"
  alt="Customers enjoying coffee on the sunny terrace"
  width="800" height="533"
  loading="lazy">
  • srcset lists candidate files, each followed by its width in pixels and the letter w.
  • sizes tells the browser how wide the image will be displayed: here half the viewport on wide screens and full width otherwise.
  • src is the fallback for browsers without support.

The browser combines this with the screen density and chooses a file. You describe the options; the browser decides.

Pixel density#

For fixed-size images such as logos, use density descriptors instead:

html
<img src="images/logo-100.png"
     srcset="images/logo-100.png 1x, images/logo-200.png 2x"
     alt="Brew and Byte logo" width="100" height="100">

The picture element#

<picture> goes further: it lets you change the format or the crop depending on conditions.

html
<picture>
  <!-- wide screens get a landscape crop -->
  <source media="(min-width: 800px)" srcset="images/hero-wide.avif" type="image/avif">
  <source media="(min-width: 800px)" srcset="images/hero-wide.jpg">
  <!-- small screens get a portrait crop -->
  <source srcset="images/hero-tall.webp" type="image/webp">
  <img src="images/hero-tall.jpg" alt="Steaming cup of coffee on a wooden table" width="600" height="800">
</picture>

The browser walks through the <source> elements in order and uses the first that matches its media query and supported type. The final <img> is mandatory: it carries the alt text and acts as the fallback.

Use srcset and sizes when you just need different sizes of the same picture. Use picture for art direction or modern formats.

figure and figcaption#

When an image (or code sample, diagram or quote) is referenced as a self-contained unit with a caption, wrap it in <figure>:

html
<figure>
  <img src="images/latte-art.jpg" alt="A latte topped with a milk-foam leaf" width="600" height="400">
  <figcaption>Our head barista, Sita, pours the signature leaf pattern.</figcaption>
</figure>

<figcaption> is the visible caption and is connected to the figure by structure. It complements alt rather than duplicating it: the alt describes what is visible for someone who cannot see, while the caption adds context for everybody.

Common mistakes#

  • Leaving out sizes, so the browser assumes full viewport width.
  • Using picture without a fallback img.
  • Repeating the caption word for word as alt text.
  • Serving only huge images and relying on CSS to shrink them.
  • Forgetting width and height, causing layout shifts.

Try it yourself#

Prepare one photo at three widths. Display it with srcset and sizes, then use Developer Tools' Network tab with different device widths to see which file loads. Next, wrap one of your images in figure with a caption, and create a picture element that shows a different crop on small screens.

Recap#

srcset and sizes let the browser choose the right image size, picture switches formats or crops with a mandatory fallback img, and figure with figcaption adds an associated caption. Always keep width, height and alt.

Check your understanding

Lesson quiz

Optional

A short quiz to check what you just learned.

Questions
4
Time limit
5 min
Pass mark
70%
Best score
-