Sub topic 12
Captions with figure and figcaption
This chapter assumes you can write an img element with src and alt, and that you know how elements nest inside each other. It covers the figure and figcaption elements, which attach a caption to self-contained content.
What a figure is
A figure element wraps content that is referenced from the main text but could be moved elsewhere without breaking the sense of the page. Typical examples are photos, charts, code listings, and quotations. An optional figcaption element gives the group a caption.
<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 figcaption must be the first or the last child of the figure. Placing it in the middle is invalid.
Caption and alt text do different jobs
The caption is visible to everyone. The alt text replaces the image for people who cannot see it. Do not copy one into the other.
altdescribes what the image shows: "Bar chart showing sales growing each quarter of 2024".figcaptiongives context or a title: "Quarterly sales, 2024".
If the caption already describes the image fully, the alt text can be shorter, but it should not be empty unless the image is purely decorative. Screen readers announce the figure and its caption, so repeating the same sentence in both places makes the page tedious to listen to.
Figures with more than images
A figure can hold any flow content. Here it wraps a code listing.
<figure>
<pre><code><a href="/docs/">Documentation</a></code></pre>
<figcaption>A link written with a root-relative URL</figcaption>
</figure>A figure can also contain several images that share one caption.
<figure>
<img src="images/step-1.jpg" alt="Empty bowl on a kitchen counter" width="300" height="200">
<img src="images/step-2.jpg" alt="Bowl filled with flour and eggs" width="300" height="200">
<figcaption>Left: before mixing. Right: after adding the eggs.</figcaption>
</figure>Common errors
- Putting
figcaptionoutside itsfigure. It only has meaning as a child offigure. - Using
figcaptionwith nofigure, or usingpfor a caption and losing the link between the caption and the image. - Putting the caption text only inside
alt, which hides it from sighted users. - Wrapping a linked image so the
figureends up inside theaincorrectly. Both nestings are valid, but be deliberate. Put theaaround theimgif only the image is clickable.
<figure>
<a href="images/red-fox-large.jpg">
<img src="images/red-fox.jpg" alt="A red fox standing in snow" width="640" height="427">
</a>
<figcaption>A red fox in winter. Select the image for the full-size version.</figcaption>
</figure>Exercise
A page shows a photo of a lighthouse with the visible caption "Cape Point lighthouse, 2023". Write the markup using figure and figcaption, with alt text that does not simply repeat the caption. Use the file images/lighthouse.jpg, width 600, height 400.
Show answer
<figure> <img src="images/lighthouse.jpg" alt="White lighthouse on a cliff above a calm sea" width="600" height="400"> <figcaption>Cape Point lighthouse, 2023</figcaption> </figure> The alt text describes what is visible, while the caption supplies the name and year. The figcaption may also come before the img.