<figure> in HTML: isolating content the surrounding text only refers to

The figure element isolates self-contained content referenced from the text, like an image, chart, or captioned code snippet.
4 min read
Believemy logo

A technical article often refers to a chart, a screenshot, or a code snippet with a sentence like as the diagram below shows. That content is not really part of the running text: it is a separate piece, one the text simply points to. It could just as well sit further down the page, in a side column, or even in an appendix, without changing the meaning of the paragraph that mentions it.

<figure> exists precisely to mark that kind of self-contained content: an image, an illustration, a code snippet, or a chart, referenced from the main text but able to stand on its own.


Definition

<figure> groups self-contained content referenced from the surrounding text, together with its caption when there is one. The test to remember is simple: if the content could be moved elsewhere in the document, or even into an appendix, without breaking the logic of the paragraph mentioning it, it is a good candidate for <figure>. On the other hand, an image that is truly part of a sentence, like an icon inline with text, has no business inside one.


What it can hold

An image is the most common case, but <figure> accepts any self-contained content: a code snippet shown inside a <pre> block, an SVG chart, a long quote, a data table, or even several images forming a small gallery under a shared caption. Its associated <figcaption>, optional but recommended, can be placed as either the first or last child.


A concrete example

An article documenting a function can illustrate its use with a captioned code snippet, rather than letting it float loosely in the middle of the text:

HTML
<p>The function below filters out empty lines before processing.</p>
<figure>
  <pre><code>for line in file: if line.strip(): process(line)</code></pre>
  <figcaption>Filtering empty lines before processing</figcaption>
</figure>

This code block could be moved into a side note or to the end of the article without the introducing sentence losing its meaning: that is exactly the sign it deserves its own <figure>.


Difference from a plain image

An <img> tag sitting alone in the text says nothing about its status: is it decorative, essential content, something that could be referenced elsewhere in the document? Nothing states it. Wrapping it in <figure> and giving it a <figcaption> gives the image its own identity: an identifiable reference point (see figure 2), an automatically associated description, and a structure that some assistive technologies let users browse independently from the rest of the text.


When to skip it

Not every image deserves a <figure>. A logo in the header, a decorative icon next to a button, or a purely illustrative photo with no caption and no explicit reference in the text gains nothing from being wrapped in this container: a plain <img> is enough, and adding <figure> around it only adds an extra tag with no real meaning.

SituationRight choice
Decorative image, no caption or referenceplain img
Icon sitting inline with textplain img
Chart referenced by the text, with a captionfigure and figcaption
Documented code snippet cited as an examplefigure and figcaption
Good to knowNot every image in an article needs a <figure>. A purely decorative photo, with no caption and no reference from the text, is simpler as a plain <img>. Reserving <figure> for genuinely self-contained content keeps the page from getting weighed down for nothing.
QuestionShould every image in an article sit inside a figure tag?

No, only the ones that count as self-contained content referenced from the text, or that need a caption. A purely decorative image, or one fully embedded in a sentence, does not need this extra container.

QuestionCan a figure hold more than one image?

Yes, that is common for a small gallery of related photos, which then share a single caption describing the set rather than one caption per image.

QuestionDoes a figure have to contain an image?

No, it can hold code, a table, a quote, a video, or an audio file: any self-contained content referenced by the text works, the image is just the most common case.

Related terms

Discover our hTML and CSS glossary

Browse the terms and definitions most commonly used in HTML and CSS development.

Share this article

Want to help us? Share this article on your networks or even better: on your site, in an article or in your newsletter.