The HTML figure tag
By Flavio
Published · Updated
Tagged: HTML
Learn how to use the HTML figure tag with figcaption to display an image alongside its caption, a semantic way to group media and its description.
Often used along with the img tag is the figure tag.
figure is a semantic tag often used when you want to display an image with a caption. You use it like this:
<figure>
<img src="dog.png"
alt="A brown dog running on a beach">
<figcaption>Dogs are allowed on this beach from October to May</figcaption>
</figure>
The figcaption tag wraps the caption text.
Where to put figcaption
figcaption must be the first or the last child of figure.
Put it first to have the caption above the image, last to have it below.
You can have only one caption per figure. If you add more, the first figcaption is the one used as the caption.
The caption is optional. A figure without a figcaption is valid HTML.
alt and figcaption are not the same thing
It’s tempting to write the same text in both. Don’t.
alt replaces the image for people that can’t see it, so it describes what’s in the picture.
figcaption is visible to everyone. Use it for what the picture alone does not tell: who took it, where, why it’s there.
If the two have the same text, a screen reader user hears it twice.
The caption also gives the figure its accessible name, which is how assistive technology announces it.
Not only for images
A figure is a piece of content that the main text refers to, and that makes sense on its own. You could move it to the side of the page, or to an appendix, and the text would still read fine.
That can be a diagram, a chart, a video, a table, a quote, or a code snippet:
<figure>
<figcaption>Get the last item of an array</figcaption>
<pre><code>const last = items.at(-1)</code></pre>
</figure>
You can also group more than one image under a single caption:
<figure>
<img src="before.png" alt="The form with all fields empty">
<img src="after.png" alt="The form with an error under the email field">
<figcaption>The form before and after validation</figcaption>
</figure>
Don’t wrap every image in a figure. An icon, a logo, or a decorative picture is not a figure. A plain img is enough.
Style a figure with CSS
Browsers add a margin to figure by default: 1em above and below, 40px on the left and on the right.
That’s why a figure looks indented compared to the paragraphs around it. Reset the side margins to align it with the text:
figure {
margin: 1em 0;
}
figure img {
max-width: 100%;
height: auto;
}
figcaption {
font-size: 0.9em;
text-align: center;
}
figure is a block element, so it’s as wide as its container. A long caption runs past the image and uses the whole line.
If you want the caption to wrap at the image width, this old trick still works for an image that has its width set:
figure {
display: table;
}
figcaption {
display: table-caption;
caption-side: bottom;
}
The figure shrinks to the image width, and the caption wraps inside it.
figure and picture
They sound similar but do different jobs.
figure adds meaning: this is a unit of content, maybe with a caption.
The picture tag is about loading: it lets the browser choose between different image files.
You can use one inside the other:
<figure>
<picture>
<source srcset="dog.avif" type="image/avif">
<img src="dog.png" alt="A brown dog running on a beach">
</picture>
<figcaption>Dogs are allowed on this beach from October to May</figcaption>
</figure>Want me to talk about your product? You can sponsor this site.