HTML Figures and Captions: Self-contained Content
Group a self-contained example
A figure represents a distinct unit of content that can be referred to from surrounding prose. It can contain a code sample, quotation, diagram or other suitable content, not only an image. Use figcaption for an optional caption explaining the unit.
<figure> <pre><code><p>Hello</p></code></pre> <figcaption>Example 1: A paragraph element.</figcaption></figure>The escaped markup is displayed as code, and the caption describes the sample as a whole.
Place the caption first or last
A figure may have one figcaption, positioned as its first or last child element. Do not place it between two pieces of figure content or add a second figcaption. It belongs inside the figure it describes.
<figure> <figcaption>Example 2: A project reminder.</figcaption> <blockquote> <p>Check the details before sharing.</p> </blockquote></figure>The caption appears before the quotation. This is also valid placement.
Do not turn every paragraph into a figure
Use ordinary paragraphs for the main narrative. Reserve figure for content that works as a unit, such as a listing that the narrative refers to. A caption is not a substitute for a section heading. A figure can also have no caption when its content is understandable without one.
<p>The checklist below summarizes the setup.</p><figure> <ol> <li>Prepare the files.</li> <li>Review the result.</li> </ol> <figcaption>Setup checklist</figcaption></figure>The surrounding paragraph remains outside the figure; the list and caption form a single unit.
Practice and next steps
Caption a code example using the character references you learned earlier. Next, learn about images and alternative text.
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
HTML Figures and Captions: Self-contained Content — Knowledge Check
Check the core concepts from this lesson.
Where may a figcaption appear inside a figure?
Which is an appropriate figure besides an image?
Why use < inside a code listing showing HTML tags?
A caption belongs to a self-contained code listing. Which structure expresses the relationship?
Can a figure be meaningful without a figcaption?