HTML figure and figcaption for Captioned Media
Lesson 19 of 36 in the HTML5 learning path.
How do I attach a caption to an image or a code sample?
figure marks self-contained media: a photo, a diagram, or a code sample that the paragraph can refer to. figcaption is the caption for that figure. The browser and assistive tools treat the caption as belonging to the media inside the figure, not as a random paragraph that happens to sit underneath.
Use a figure when the content can move to an appendix and the article still reads. A decorative icon in a button is not a figure. An image that is the content of the sentence needs alt text as well. The caption does not replace alt.
Syntax & Parameters
<figure><img alt=""><figcaption></figcaption></figure>
Example
Interactive preview available<article>
<h1>Lab photo notes</h1>
<p>The setup below is the one we use for the Friday demo.</p>
<figure>
<img src="/images/lab-bench.jpg" width="640" height="360" alt="A labeled breadboard on a wooden lab bench">
<figcaption>Fig. 1. Bench layout for the input-voltage demo.</figcaption>
</figure>
<p>Students should match the wire colors in the photo before they power the board.</p>
<figure>
<pre><code>const voltage = 3.3;</code></pre>
<figcaption>The constant used in the starter sketch.</figcaption>
</figure>
</article>
Expected Output
A heading, a sentence, a photo with "Fig. 1. Bench layout for the input-voltage demo." underneath, then another sentence, then a one-line code block with the caption "The constant used in the starter sketch."
Line-by-Line Walkthrough
- The article has a heading and a lead paragraph that points at the figure.
- figure wraps the image and its caption so they stay one unit.
- width and height reserve space so the caption does not jump when the image loads.
- alt describes the photo for someone who cannot see it.
- figcaption is the visible caption, including the figure number.
- The following paragraph continues the lesson and can say "the photo" because the figure is adjacent.
- A second figure wraps a short code sample.
- That figcaption names the snippet. The code itself is not the caption.
- Screen readers can move by figure and hear the caption with the content.
- Do not put the only description of the image in the caption and leave alt empty unless the image is described in the caption and you set alt to an empty string on purpose for a redundant case you have tested.
Common Pitfalls & Security
A caption is visible text. alt is the text alternative of the image. They do different jobs.
- Using figure around every image, including button icons, adds noise for assistive tech.
- Putting the caption in a p after the image loses the programmatic link.
- Copying the same words into alt and figcaption makes screen-reader users hear them twice. Write alt for the image and the caption for the lesson.
- A figcaption placed outside the figure is just a paragraph. It must be inside.
- User-supplied captions still need to be escaped by the template so a caption cannot inject markup.
- An image without width and height shifts the caption down after load.
Best Practices & Modern Standards
HTML5 figure is for referenced content, not for layout boxes. One figcaption per figure is the supported pattern. Place it as the first or last child so it is easy to find.
When a CMS prints the figure, escape the caption and keep the image URL on a host you trust. The caption can include a cite element for the source of a quotation inside the figure.
Real-World Use
A lab handout shows the bench photo and the starter line of code. Each has a numbered caption so the instructor can say "look at figure 1" in class and the student can find it without guessing which paragraph is the caption.
Frequently Asked Questions
Can a figure contain more than an image?
Yes. A figure can hold a code block, a table, a video, or several images that share one caption. The caption still describes that group.
Does figcaption replace alt?
No. alt is read as the image. The caption is an extra label. A meaningful photo needs a useful alt even when a caption is visible.
Where can figcaption sit?
As the first or last child of the figure. Putting it outside breaks the relationship, even if the CSS still looks attached.
Should decorative images use figure?
No. Decorative images use an empty alt and no figure. Reserve figure for content the writing refers to.