Back to HTML

Using figure and figcaption for Captions

Coding Tutorials Team 10 July 2026

Putting a caption under an image is easy to do badly. A paragraph placed after the image looks correct and records no relationship between the two, so nothing except visual proximity connects them. The figure element exists to make that connection explicit.

The basic structure

A figure wraps the content, and a caption element inside it provides the label.

<figure> <img src="/img/dom-tree.png" alt="A tree diagram showing nested HTML elements" width="800" height="500"> <figcaption>The document tree the browser builds from the markup above.</figcaption> </figure>

The caption may be the first or the last child of the figure, and there can only be one. Placing it first is the correct way to produce a caption above the image, rather than styling it into position with CSS.

What a figure is for

The definition is broader than images. A figure is self contained content referenced from the main flow, which could be moved elsewhere on the page without breaking the surrounding text.

That covers illustrations, diagrams, photographs, code listings, tables, charts and quoted passages. The test is whether the content is referred to rather than read in sequence. A diagram you discuss in the text is a figure. A decorative banner across the top of a page is not, because nothing refers to it.

Caption and alt text say different things

This is the distinction that matters most and is missed most often. The alt attribute replaces the image for people who cannot see it. The caption accompanies the image for everyone.

Because a screen reader reads both, duplicating one into the other produces the same sentence twice in a row. The caption should add information the image does not carry on its own: a source, a date, an explanation of what to notice, a figure number.

<figure> <img src="/img/chart.png" alt="Bar chart with page load times falling from 4.2 to 1.1 seconds"> <figcaption>Load times after image optimisation, measured over six weeks.</figcaption> </figure>

The alt text describes what the chart shows. The caption explains what it is and where it came from. Neither repeats the other, and someone hearing both receives a complete picture.

When the caption is the description

Occasionally a caption fully describes the image, and repeating it in the alt attribute would add nothing. In that case an empty alt attribute is defensible, since the caption is already announced and is available to everyone rather than only to screen reader users.

This works only when the caption genuinely conveys everything the image does. If the picture carries detail the caption omits, the alt text still has a job to do.

A figure does not require a caption

The caption is optional. A figure with no caption still marks the content as a self contained unit that could be relocated, which is useful structural information even without a visible label. In practice most figures have captions, because the reason to reach for the element is usually that you want one.

Code listings and tables

A figure around a code block with a caption naming what it demonstrates is a tidy pattern for tutorials, and it gives you a single element to style and to reference.

Tables are a slightly different case, because they have their own caption element that is more strongly associated for assistive technology. Prefer the table caption when the label describes the table itself, and use a figure when you want to group a table with something else, such as a source note or an accompanying chart.

Numbering figures

On technical pages where the text says see figure three, the number needs to exist. It can be written into the caption directly, which is simple and reliable, or generated with a CSS counter, which keeps the numbering correct automatically when figures are added or reordered.

Generated content is not always exposed consistently to assistive technology, so if the number is referenced in the prose it is safer to have it in the markup. Decorative numbering is fine to generate.

Multiple images in one figure

A figure may contain several images sharing a single caption, which is the correct structure for a before and after pair or a small gallery illustrating one point. The caption then describes the group rather than any individual picture, and each image still carries its own alt text.

Styling notes

Browsers apply a default horizontal margin to figures, which surprises people who expect it to sit flush. Resetting the margin is usually the first thing you do.

Beyond that a figure is an ordinary block element and can be floated, made a grid item or centred like anything else. Constraining the caption width to match the image rather than the container is a small detail that makes a page look considerably more finished.