Sub topic 12
Captions with figure and figcaption
This chapter assumes you can write an img element with src and alt, you know how width and height work, and you can open an HTML file in a browser. It covers the figure and figcaption elements: what they mean, how to use them, and the mistakes that come up most often.
What figure is for
The figure element wraps self-contained content that is referred to from the main text. The content could be removed from the flow of the page, moved to an appendix, and the text would still make sense. Typical examples are:
- An image, chart, or photograph.
- A code listing.
- A poem or a quotation with attribution.
- A table or a diagram.
- A group of related images.
The figcaption element gives the figure a caption. It must be a direct child of figure, and it must be the first or the last child.
<figure>
<img src="images/chart.png" alt="Bar chart showing sales growing each quarter of 2024" width="800" height="450">
<figcaption>Quarterly sales, 2024</figcaption>
</figure>The figure element does not change how the image loads. It adds meaning. Assistive technology can announce a figure and its caption as a unit, and the browser knows the caption belongs to the image.
The caption and the alt text do different jobs
Beginners often think the caption replaces alt, or that alt can be left empty because a caption exists. Neither is correct.
altdescribes the image for someone who cannot see it. It is used when the image fails to load and by screen readers.figcaptionis visible to everyone. It adds context: a title, a source, a date, or a credit.
In the example above, the alt text states what the chart shows. The caption names the chart. If both said "Quarterly sales, 2024", a screen reader user would hear the same words twice.
| Question | alt | figcaption |
|---|---|---|
| Who sees it? | Screen readers, and everyone if the image fails | Every visitor |
| Purpose | Replace the image | Add context to the image |
| Required? | Yes, on every `img` | No |
| Can be empty? | Yes, for decorative images | An empty caption is pointless, so leave it out |
A good test: read the alt text and caption together, with the image hidden. If the pair sounds repetitive, shorten one of them.
Structure rules
The content model of figure is simple, but a few details matter.
figcaptionis optional. Afigurewithout a caption is valid.- There can be at most one
figcaptionin afigure. figcaptionmust be the first or last child offigure. It cannot sit between two images.- A
figurecan contain any flow content, not only images.
Placing the caption first is valid, and it suits tables and code, where the title usually comes before the content.
<figure>
<figcaption>Listing 1: A minimal HTML page</figcaption>
<pre><code><!doctype html>
<html lang="en">
<title>Hello</title>
<p>Hello, world.</p>
</html></code></pre>
</figure>Note that the code is escaped. Inside code, the characters < and > must be written as < and >, or the browser will treat them as real tags.
Figures with several images
One caption can serve a group of images that belong together, such as before-and-after photos. Each image still needs its own alt text.
<figure>
<img src="images/room-before.jpg" alt="Living room with bare walls and a worn carpet" width="400" height="300">
<img src="images/room-after.jpg" alt="Same living room with blue walls and a wooden floor" width="400" height="300">
<figcaption>The living room before and after renovation</figcaption>
</figure>If the images are separate ideas that happen to sit near each other, use one figure per image instead. Ask whether each image would still make sense if the other were moved elsewhere.
Combining figure with links
You can link the image inside a figure, for example to a larger version. Put the a element around the img, not around the figure.
<figure>
<a href="images/chart-large.png">
<img src="images/chart.png" alt="Bar chart showing sales growing each quarter of 2024" width="800" height="450">
</a>
<figcaption>Quarterly sales, 2024. Select the chart for a larger version.</figcaption>
</figure>The image is the only content of the link, so its alt text becomes the link text. A screen reader will announce a link with that description. If the link goes somewhere other than the image itself, say so in the caption or the alt text.
How figure fits into the page
The following diagram shows the structure of a figure and which parts are visible, which are read by assistive technology, and which load as a file.
Common errors
- Using figure for layout.
figureis not a generic box for styling. If the content is not referred to from the text, use adiv. - Wrapping every image in figure. A logo, an icon, or a decorative divider is not a self-contained piece of content. Leave those as plain
imgelements. - Putting figcaption in the wrong place. A caption in the middle of a figure, or outside it, is invalid and loses its link to the content.
- Using a paragraph as a caption. A
pbelow an image looks like a caption, but nothing tells the browser or a screen reader that it is one. - Repeating the caption as alt text. This makes screen reader users hear the same thing twice.
- Dropping alt because there is a caption. An
imgstill needsalt. For an image that is purely decorative, usealt=""and skip the figure.
Referring to a figure from the text
Because a figure is referred to from the main text, you will often want a link to it. Give the figure an id and use a fragment link, as covered earlier in this tutorial.
<p>Sales rose every quarter, as <a href="#fig-sales">Figure 1</a> shows.</p>
<figure id="fig-sales">
<img src="images/chart.png" alt="Bar chart showing sales growing each quarter of 2024" width="800" height="450">
<figcaption>Figure 1: Quarterly sales, 2024</figcaption>
</figure>Write the figure number as plain text in the caption. HTML does not number figures for you. If you add or remove a figure, you must renumber by hand, or generate the numbers with CSS counters or a build tool.
Checklist
Before you publish a figure, check these points:
- Does the content stand on its own and get referred to from the text?
- Is
figcaptionthe first or last child, and is there only one? - Does every
imghave meaningfulalt, different from the caption? - Do images have
widthandheightto prevent layout shift? - If the image is linked, does the link text (the alt text) say where it goes?
Exercise
A page has a photo of a lighthouse inside a figure. The figcaption reads "Lighthouse at Cape Point, photographed in 2023" and the img has alt="Lighthouse at Cape Point, photographed in 2023". What is wrong, and how would you fix it?
Show answer
The alt text repeats the caption, so a screen reader user hears the same sentence twice. The alt text should describe what the image shows, and the caption should add context. For example: alt="White lighthouse on a cliff above a calm sea", with the caption "Lighthouse at Cape Point, photographed in 2023".
Exercise
Write the markup for a figure with a table of three product prices, where the caption "Table 1: Product prices" appears above the table. Is this valid, and why?
Show answer
Yes, it is valid because figcaption may be the first child of figure. Example: <figure><figcaption>Table 1: Product prices</figcaption><table><tr><th>Product</th><th>Price</th></tr><tr><td>Pen</td><td>$2</td></tr><tr><td>Notebook</td><td>$5</td></tr></table></figure>
Exercise
You want a captioned chart that visitors can select to open a larger version. Where does the a element go, and what becomes the link text?
Show answer
Put the a element inside the figure, around the img only. The img is the sole content of the link, so its alt text becomes the link text, and it should describe the chart. The figcaption stays outside the a element.