LEARNING OBJECTIVES ⌵
- Understand the semantic definition and WHATWG specification rules of the
<figure>and<figcaption>elements. - Differentiate between an image's accessible alternative text (
alt) and its visible caption (<figcaption>). - Master the syntax rules for placing
<figcaption>as either the first or last child of a<figure>. - Encapsulate non-image content (code listings, ASCII diagrams, data tables, quotes) inside semantic
<figure>elements. - Understand how assistive technologies (Screen Readers) expose the
figurelandmark role and its accessible caption label.
📖 The Mental Model & Story (Intuitive Foundation)
Imagine reading a physical scientific textbook or an issue of National Geographic.
As you read a paragraph about volcanic eruptions, the text says: (See Figure 4.2 for magma chamber cross-section).
On the next page, or in a sidebar box, you see a bordered box containing a detailed geological diagram. Directly underneath the diagram is a printed italicized text box that reads:
Figure 4.2: Subduction zone magma chamber cross-section showing tectonic plate boundary.
Notice two key architectural characteristics:
- Self-Contained Unit: The diagram and its caption form a complete, independent unit of knowledge.
- Relocatable: If the magazine editor moved Figure 4.2 to the appendix, the main text of the article would still make complete sense without grammatical interruption.
+-------------------------------------------------------------+
| MAIN ARTICLE BODY TEXT |
| The subduction zone creates intense friction... |
| |
| +-------------------------------------------------------+ |
| | <figure> | |
| | [ GEOLOGICAL CROSS-SECTION DIAGRAM ] | |
| | | |
| | <figcaption> | |
| | Figure 4.2: Magma chamber cross-section | |
| | </figcaption> | |
| | </figure> | |
| +-------------------------------------------------------+ |
| |
| ...as demonstrated in recent seismic surveys. |
+-------------------------------------------------------------+
In HTML5, <figure> is the semantic container that represents self-contained flow content, optionally associated with a <figcaption>.
Technical Deep Dive & Specifications
The WHATWG HTML Specification Rules
<figure>: Represents any self-contained content (images, diagrams, photos, code snippets, quotes, math formulas) that is referenced from the main text, but could be moved to another part of the document (or an appendix) without affecting the flow of the document.- Permitted Parents: Any element that accepts flow content.
- ARIA Role:
figure(Implicit).
<figcaption>: Represents a caption or legend for the rest of the contents of its parent<figure>element.- Syntax Constraint: Must be either the very first child OR the very last child of the
<figure>. - Quantity Constraint: At most one
<figcaption>per<figure>. - ARIA Mapping: Automatically computes as the accessible label (
aria-labelledby) for the parentfigurerole in the Accessibility Tree.
- Syntax Constraint: Must be either the very first child OR the very last child of the
VALID FIGCAPTION POSITION (TOP): VALID FIGCAPTION POSITION (BOTTOM):
<figure> <figure>
<figcaption>Caption</figcaption> <img src="..." alt="...">
<img src="..." alt="..."> <figcaption>Caption</figcaption>
</figure> </figure>
[!CAUTION] Placing
<figcaption>in the middle of a figure (e.g. between two images) is invalid HTML5 syntax. It must strictly be the first or last child.
alt Attribute vs. <figcaption>: The Crucial Difference
| Feature | alt Attribute |
<figcaption> Element |
|---|---|---|
| Visibility | Invisible to sighted users (unless image breaks). | Visible text rendered directly on the web page. |
| Purpose | Non-visual text substitute for the image. | Explanatory context, legend, citation, or attribution. |
| Target Audience | Screen readers, search bots, broken image states. | Sighted readers and screen reader users alike. |
| Allowed Content | Plain text string only (no HTML tags). | Full HTML flow content (links, <strong>, <em>, <code>). |
Figures Beyond Images: Code Listings & Data Tables
<figure> is not restricted to images. It is the semantic container for any relocatable asset:
<!-- Semantic Code Listing Figure -->
<figure>
<figcaption><code>Listing 3.1:</code> Rust Async Channel Implementation</figcaption>
<pre><code class="language-rust">
async fn process_messages(mut rx: Receiver<Message>) {
while let Some(msg) = rx.recv().await {
println!("Received: {:?}", msg);
}
}
</code></pre>
</figure>
💻 Interactive Code Playground
Starter Code
Line-by-Line Code Breakdown
- Line 47 (
<figure class="editorial-figure">): Wraps the media and its description in a single semantic landmark node with the implicit ARIA role offigure. - Line 48–53 (
<img ...>): The primary visual payload. Contains its own distinct, non-visual description in thealtattribute. - Line 54–57 (
<figcaption>): The human-readable visible caption, including strong figure numbering (Figure 1.1) and photo attribution.
Expected Browser Render Output
+-------------------------------------------------------------+
| The James Webb Infrared Revelations |
| |
| Recent observations of the Carina Nebula have revealed... |
| |
| +---------------------------------------------------------+ |
| | [ HIGH-RES INFRARED NEBULA IMAGE ] | |
| | ------------------------------------------------------- | |
| | Figure 1.1: The Cosmic Cliffs of NGC 3324 captured in | |
| | near-infrared light. Credit: NASA, ESA, CSA, and STScI. | |
| +---------------------------------------------------------+ |
| |
| By detecting longer wavelengths of infrared radiation... |
+-------------------------------------------------------------+🏋️ Hands-On Exercise
🎯 The Challenge: Build a Multi-Image Comparison Figure
Instructions:
- Create a single
<figure>container that presents a side-by-side comparison of two satellite images (Before & After reforestation). - Place two
<img>elements side by side inside the<figure>. - Add a
<figcaption>at the top of the figure that clearly explains what the two images depict. - Ensure both images have distinct
altattributes describing their individual visual states. - Use CSS Grid or Flexbox to place both images on a 2-column horizontal row.
🏁 Starter Code Sandbox
⚠️ Common Pitfalls
- Placing
<figcaption>Outside of<figure>: A<figcaption>element is only valid when nested directly inside a<figure>. Using<figcaption>inside a standard<div>or<section>is invalid HTML. - Multiple
<figcaption>Elements: Writing more than one<figcaption>inside a single<figure>is invalid. If you have multiple images, wrap their sub-labels in<p>or<span>and keep one primary<figcaption>. - Using
<figure>for Purely Decorative Graphics: Do not wrap every decorative background flourish or social media icon inside<figure>. Reserve<figure>for self-contained informational content. - Duplicating
altText Verbatim in<figcaption>: Sighted screen reader users will hear the exact same sentence read twice in immediate succession. Ensurealtdescribes the raw visual data and<figcaption>provides editorial context.
💡 Pro Tips
- Default Browser Margins: Browsers apply a default user-agent style
margin: 1em 40px;on<figure>elements. Always resetfigure { margin: 0; }in your CSS reset stylesheets to prevent unwanted indentation. - Accessibility Tree Pairing: When a screen reader navigates to a
<figure>, it announces: "Figure, [Figcaption Text]". This provides effortless landmark navigation for research papers and textbooks. - Non-Image Figure Use Cases: Use
<figure>for interactive data visualizations (<canvas>or<svg>), audio/video clips with transcripts, blockquotes with citations, or syntax-highlighted code blocks.
📌 Key Takeaways
<figure>represents self-contained, relocatable content (images, code, tables, charts).<figcaption>provides a visible caption and must be either the first or last child of<figure>.- There can be at most one
<figcaption>per<figure>. - A single
<figure>can group multiple related images or charts under one unified caption. altis the non-visual text substitute;<figcaption>is the visible editorial context.- --