How do I group a figure with its caption in HTML?
Quick answer
Wrap self-contained content, such as an image or diagram, in <figure>, then put its caption in a <figcaption> inside that element. Place the caption first or last within the figure. A paragraph that merely appears nearby is not associated as the figure’s caption; keep it outside unless it is genuinely caption text. [S1]
The question, the details, the next step.
A practical checklist
Identify the image, diagram, code example, or other self-contained item you want to group.
Wrap that item in one
<figure>element.Put the caption text in a
<figcaption>inside that same figure.Place
<figcaption>as the first or last child of the figure.Keep broader explanation in a separate paragraph rather than presenting it as the figure caption.
A few more useful answers
Can `<figcaption>` come before the image?
Yes. It can be the first or last child inside <figure>. [S1]
Does a paragraph right below a figure count as its caption?
No, not just because it is nearby. Put caption text in <figcaption> inside the <figure> to associate it as the caption. [S1]
Is `<figure>` only for photographs?
No. It can group other self-contained content, including illustrations, diagrams, and code snippets. [S1]
Sources
References used for this guide.
- [S1] The HTML figure element — MDN developer.mozilla.org · Checked
