Skip to content
Tutorials Center
My learning

Topics

Lesson 35 of 122 Β· Images and Media

Figures and Captions

Use figure and figcaption to caption images, code samples, quotes and charts. Learn when content is a figure and how alt text and captions differ.

Books, newspapers and scientific papers have always set certain content apart: a photo with a line of text beneath it, a numbered diagram, a code listing with a label. HTML has a pair of elements for exactly this: <figure> and <figcaption>.

In this lesson you will learn what makes something a figure, how to caption images, code, quotations and charts, and how a caption relates to an image's alt text β€” two things that are easy to confuse.

What is a figure?

A <figure> is a piece of self-contained content that the main text refers to, but that could be moved somewhere else β€” to the side, to the next page, to an appendix β€” without breaking the flow of the text. Think "see Figure 3" in a textbook.

Good candidates:

  • A photo or illustration that supports an article.
  • A diagram or chart with its data source.
  • A code listing that the text discusses.
  • A quotation presented on its own with its attribution.
  • A video or audio clip with a description.

The test is simple: could you move it and replace it with a reference like "(see the chart below)"? If yes, it is probably a figure. If the content is part of the sentence flow β€” a small inline icon, a logo in the header, a photo that is the main content of a gallery page β€” it is not.

<figure> and <figcaption>

<figcaption> holds the caption. It must be the first or last child of the <figure>, and a figure can have at most one caption. The caption is optional; a figure without one is still valid.

A captioned image

Browsers give <figure> some default margins (usually 40 pixels on the left and right), which is why many stylesheets reset them as above. The caption itself has no special styling: it is up to your CSS to make it look like a caption.

The pairing is more than visual. Assistive technology exposes the figure as a group and uses the <figcaption> as its accessible name, so a screen reader user hears the caption as a label for the whole figure.

Caption first or last?

Put the <figcaption> wherever it reads best. Photo captions usually go below; table-like content and code listings often have their label above:

HTML
<figure>
  <figcaption>Listing 2: The smallest valid HTML document.</figcaption>
  <pre><code>&lt;!doctype html&gt;
&lt;title&gt;Hi&lt;/title&gt;</code></pre>
</figure>

alt text vs. captions

This is the most common source of confusion. An image inside a figure needs both, and they do different jobs:

alt <figcaption>
Who sees it? Only people who cannot see the image (and search engines) Everyone
What does it do? Replaces the image Adds context to the image
Typical content What the image shows Who, where, when, why, credit, source

Think of it this way: if the image vanished, the alt text should take its place, and the caption should still make sense next to it. The two should not say the same thing β€” otherwise a screen reader user hears it twice.

HTML
<figure>
  <img src="bridge.jpg"
       alt="A narrow wooden footbridge crossing a rocky stream in a forest"
       width="1200" height="800">
  <figcaption>The old mill bridge, rebuilt by volunteers in 2024. Photo: Ana Ruiz.</figcaption>
</figure>

The alt describes what is in the picture. The caption tells you what you could not know just by looking: its name, its history, who took the photo.

What if the caption already describes everything? If the caption fully conveys the image (for example, a chart whose caption states every number), the alt can be shorter and point to it, such as alt="Bar chart; details in the caption". Avoid an empty alt on informative images β€” that tells assistive technology the image is decorative.

Charts and diagrams

Charts are a natural fit for figures. The alt gives the headline finding; the caption names the chart and its source. For complex data, also provide the numbers as text β€” a table works well β€” so nobody depends on reading the picture.

A chart figure

A caption can contain any flow content: paragraphs, links, even a <details> disclosure as here. You will learn more about that element in Collapsible Sections with Details.

Code and quotations

Code listings

When an article discusses a piece of code at length, wrapping it in a figure gives it a label you can refer to. The code itself still uses <pre> and <code>:

Captioned code and quote

Quotations with attribution

The second figure above shows a pattern for pull quotes and testimonials: a <blockquote> holds the quoted words, and the <figcaption> holds the attribution. This keeps the person's name outside the blockquote, which is correct: a blockquote should contain only the quoted text, and the speaker did not say their own name.

Several images in one figure

A figure can group multiple images under one caption, for example a before-and-after comparison:

HTML
<figure>
  <img src="room-before.jpg" alt="An empty attic with bare wooden beams" width="600" height="400">
  <img src="room-after.jpg" alt="The same attic as a bright reading room with a sofa and shelves" width="600" height="400">
  <figcaption>The attic before and after the renovation.</figcaption>
</figure>

Each image keeps its own alt. The single caption describes the group.

When not to use <figure>

  • Every image on the page. A decorative banner or an icon is not a figure. A plain <img> is fine.
  • Just to get a caption look. If the content is not self-contained, styling a paragraph is more honest than claiming it is a figure.
  • For layout. <figure> says something about meaning, not position. Use CSS for placement, as in Page Layout.

Figures and tables. A data table has its own caption element, <caption>, covered in Table Headers, Sections and Captions. Use that for the table title; you only need a <figure> around a table if you want to treat it as a numbered figure with extra notes.

Check your understanding

Quick check Β· 4 questions

of 4 answered You scored / 4 Β· perfect!

  1. 1. Where may a figcaption appear inside a figure?

    The figcaption must be the first or last child of its figure, and there can be at most one.
  2. 2. An image of a bridge has the caption "Rebuilt by volunteers in 2024. Photo by Ana Ruiz." What is the best alt text?

    The alt replaces the image; the caption adds context. Repeating the caption makes screen readers say it twice, and an empty alt would mark the photo as decorative.
  3. 3. You are adding a testimonial with the customer's name. Where should the name go?

    The blockquote holds only the quoted words. The figcaption is the natural place for the attribution. The cite attribute is for a source URL, not a name.
  4. 4. Which of these is the best candidate for a figure?

    The diagram is self-contained and referenced from the text, so it could move without breaking the reading flow. Logos and icons are part of the interface, not figures.

Summary

  • <figure> marks self-contained content that the text refers to and that could be moved elsewhere.
  • <figcaption> is optional, goes first or last in the figure, and labels the whole figure.
  • Images inside figures still need alt: alt replaces the image, the caption adds context. Do not duplicate them.
  • Figures suit photos, charts, diagrams, code listings and quotations with attribution.
  • Do not wrap every image in a figure, and do not use figures for layout.

Next up: making different parts of one image clickable with Image Maps.