Lesson 20 of 51 · Module 3, lesson 5 of 6 · 3 min

Figures and captions

Give an image a visible caption, and understand how a caption differs from alt text.

What you'll learn

  • How to give an image a visible caption with <figure> and <figcaption>
  • The difference between a caption and alt text
  • What else can go in a figure

Why this matters

A caption tells every reader — sighted or not — what they are looking at, or why it matters. HTML has a pair of elements that tie the caption to its image, so that browsers and screen readers know they belong together.

A figure with a caption

html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Fresh bread</title>
</head>
<body>
  <h1>Fresh bread</h1>
  <figure>
    <img src="images/bread.jpg" alt="A round golden loaf of bread with three cuts on top" width="300" height="200">
    <figcaption>The loaf after 40 minutes in the oven.</figcaption>
  </figure>
  <p>Bake the bread until the top is golden.</p>
</body>
</html>

What you'll see: the image, with the caption in normal text right under it. The whole figure is moved in a little from the left edge (the browser's default look).

The page "Fresh bread" with a drawing of a golden loaf, indented from the left, and under it the caption "The loaf after 40 minutes in the oven." Below the figure, the sentence "Bake the bread until the top is golden."

  • <figure> wraps the whole thing.
  • <figcaption> holds the caption. Put it first or last inside the <figure>.

Caption or alt?

They do different jobs, so a figure usually needs both:

alt<figcaption>
Who gets itPeople who cannot see the imageEveryone
What it saysWhat the image showsExtra information: who, when, why it matters
Visible?No (unless the image fails to load)Yes

Do not copy the same words into both. The screen reader user would hear them twice.

What can be a figure?

A figure is any content that the main text refers to, but that could move — for example to the side, or to the next page — without breaking the text. That can be a photo, a chart, a code example or a quotation:

html
<figure>
  <pre><code>&lt;p&gt;Hello!&lt;/p&gt;</code></pre>
  <figcaption>The smallest useful HTML element.</figcaption>
</figure>

Not every image needs to be a figure. A small icon or a decorative image stays a plain <img>.

Try it yourself

  1. Select the complete example and choose Try it.
  2. Move the <figcaption> above the <img> and see where the caption appears.
  3. Make a second figure with images/salad.jpg, a good alt, and a caption that adds something the image does not show — for example, who made the salad.

Common mistakes

  • Using a normal paragraph as a caption. It looks right but is not connected to the image. Fix: use <figcaption> inside <figure>.
  • The same text in alt and in the caption. Fix: alt describes; the caption adds.
  • Leaving out alt because there is a caption. The image still needs a description. Fix: keep both.
  • More than one <figcaption> in a figure. Only one is allowed. Fix: combine the text.

Check yourself

  1. Which element holds the caption of a figure?
  2. Should a figure's alt and caption use the same words?
  3. Can a code example be a figure?

Answers

  1. <figcaption>, inside <figure>.
  2. No. alt describes the image; the caption adds information for everyone.
  3. Yes. A figure can hold images, charts, code or quotations.

Next: one image size does not fit every screen. The next lesson (optional) shows how to give phones a smaller file.

Chat with us on WhatsApp