figure
The figure element represents self-contained content with an optional caption, used for images, diagrams, code snippets, and quotations.
Description
The <figure> element represents self-contained content that is referenced from the main content but could be moved to an appendix or sidebar without affecting the document flow. It typically contains images, diagrams, code listings, or quotations along with an optional <figcaption>.
Vanilla Breeze provides variants for bordered figures, floating figures, code examples, and pull quotes.
When to Use
- Images with captions - Photos, illustrations, diagrams with descriptive text
- Code examples - Code snippets with file names or descriptions
- Quotations - Pull quotes or block quotes with attribution
- Videos - Embedded videos with titles or descriptions
- Charts and graphs - Data visualizations with explanatory captions
- Tables - Data tables with descriptive captions
When Not to Use
- Decorative images that don't need captions
- Inline images that are part of the text flow
- Content that can't stand on its own
Variants
Default
Basic figure with content and optional caption.
<figure> <img src="landscape.jpg" alt="A scenic landscape" /> <figcaption>A beautiful mountain landscape at sunset.</figcaption></figure>
.bordered
Adds padding and border for a card-like appearance with separated caption.
<figure class="bordered"> <img src="photo.jpg" alt="..." /> <figcaption>Caption with border separator.</figcaption></figure>
.centered
Centers the figure and caption for standalone display.
<figure class="centered"> <img src="photo.jpg" alt="..." style="max-inline-size: 400px;" /> <figcaption>A centered figure with centered caption.</figcaption></figure>
.float-start
Floats the figure to the start (left in LTR) with text wrapping.
<figure class="float-start" style="max-inline-size: 200px;"> <img src="photo.jpg" alt="..." /> <figcaption>Caption text</figcaption></figure><p>Text wraps around the figure...</p>
.float-end
Floats the figure to the end (right in LTR) with text wrapping.
<figure class="float-end" style="max-inline-size: 200px;"> <img src="photo.jpg" alt="..." /> <figcaption>Caption text</figcaption></figure><p>Text wraps around the figure...</p>
.full
Full container width.
<figure class="full"> <img src="panorama.jpg" alt="..." /> <figcaption>A full-width figure spanning the entire container.</figcaption></figure>
Special Variants
.code
Styled for code examples with filename in caption.
function greet(name) {
return `Hello, ${content}#123;name}!`;
}
console.log(greet('World'));
<figure class="code"> <pre><code>function greet(name) { return `Hello, ${name}!`;}</code></pre> <figcaption>example.js</figcaption></figure>
.quote
Styled for pull quotes with attribution.
The only way to do great work is to love what you do.
<figure class="quote"> <blockquote> The only way to do great work is to love what you do. </blockquote> <figcaption>Steve Jobs</figcaption></figure>
Sticky Caption
When a figure contains a horizontally-scrollable element like a wide table or code block, the <figcaption> automatically stays anchored at the left edge while the content scrolls. This is applied automatically — no class or attribute needed.
const config = { entry: "./src/index.js", output: { path: "/dist", filename: "bundle.js" }, module: { rules: [{ test: /\.css$/, use: ["style-loader", "css-loader"] }] } };
Content Types
Figures can contain various types of content beyond images.
Image
<figure> <img src="photo.jpg" alt="Example image" /> <figcaption>Figure 1: A sample image with caption.</figcaption></figure>
Picture (Responsive Image)
<figure> <picture> <source media="(min-width: 600px)" srcset="photo-wide.jpg" /> <img src="photo-narrow.jpg" alt="Responsive image" /> </picture> <figcaption>Figure 2: A responsive image using the picture element.</figcaption></figure>
Video
Use the poster attribute and a fallback paragraph for browsers without video support.
<figure> <video controls preload="none" poster="poster.jpg"> <source src="intro.mp4" type="video/mp4" /> <p>Your browser does not support the video element. <a href="intro.mp4">Download the video</a> instead.</p> </video> <figcaption>Figure 3: Introduction video with playback controls.</figcaption></figure>
Canvas
Canvas elements require JavaScript to draw content. The figure wraps the canvas and provides a caption describing the visualization.
<figure> <canvas id="chart" width="500" height="260"></canvas> <figcaption>Figure 4: Quarterly revenue (thousands).</figcaption></figure>
SVG
Inline SVG diagrams benefit from role="img" and an aria-label for accessibility.
<figure> <svg viewBox="0 0 400 100" role="img" aria-label="Five squares showing decreasing opacity"> <rect x="10" y="20" width="60" height="60" fill="var(--color-interactive)" rx="4" /> <rect x="90" y="20" width="60" height="60" fill="var(--color-interactive)" rx="4" opacity="0.8" /> <rect x="170" y="20" width="60" height="60" fill="var(--color-interactive)" rx="4" opacity="0.6" /> <rect x="250" y="20" width="60" height="60" fill="var(--color-interactive)" rx="4" opacity="0.4" /> <rect x="330" y="20" width="60" height="60" fill="var(--color-interactive)" rx="4" opacity="0.2" /> </svg> <figcaption>Figure 5: SVG diagram showing opacity progression.</figcaption></figure>
Iframe
Wrap iframes in a responsive container to maintain aspect ratio. Always include a descriptive title attribute.
<figure> <div class="embed-responsive"> <iframe src="page.html" title="Embedded content"></iframe> </div> <figcaption>Figure 6: Embedded external content.</figcaption></figure>
Audio
Native audio controls render even without a source file. Include a fallback message for older browsers.
<figure> <audio controls> <source src="podcast.mp3" type="audio/mpeg" /> <p>Your browser does not support the audio element.</p> </audio> <figcaption>Audio 1: Podcast episode — "Introduction to Web Development"</figcaption></figure>
Accessibility
Figure Role
The <figure> element has an implicit figure role when it contains a <figcaption>. Screen readers announce it as a figure with its caption.
Alt Text vs Caption
The alt attribute and <figcaption> serve different purposes:
alt- Describes the image content for screen readersfigcaption- Provides context visible to all users
<figure> <!-- alt describes WHAT the image shows --> <img src="chart.png" alt="Bar chart comparing Q1-Q4 sales" /> <!-- figcaption provides CONTEXT about why it's shown --> <figcaption>Figure 1: Q4 sales exceeded targets by 15%.</figcaption></figure>
Best Practices
- Always include
alttext on images within figures - Don't repeat the same information in
altandfigcaption - Use
figcaptionfor context, attribution, or additional details - Number figures if referenced in text (Figure 1, Figure 2)
- Ensure figures make sense when read out of document order
Related
<figcaption>- Caption element for figures<img>- Images within figures<picture>- Responsive images within figures<video>- Videos within figures<audio>- Audio within figures<canvas>- Canvas graphics within figures<svg>- SVG diagrams within figures<blockquote>- Quotations within figures
CSS Reference
Styles defined in /src/native-elements/figure/styles.css
| Selector | Properties |
|---|---|
figure |
display: block; margin: 0; |
figure > img/video/picture/iframe/canvas |
display: block; inline-size: 100%; block-size: auto; |
figure.bordered |
padding; border; border-radius; |
figure.centered |
margin-inline: auto; text-align: center; |
figure.float-start |
float: inline-start; max-inline-size: 50%; |
figure.float-end |
float: inline-end; max-inline-size: 50%; |
figure.code |
Special pre/code/figcaption styling; |
figure.quote |
Special blockquote/figcaption styling; |
figure:has(:is(table, pre)) > figcaption |
position: sticky; inset-inline-start: 0; |