<figure>
A self-contained illustration with an optional caption: an image, chart, code snippet, or quote.
- Display
- block
- Closing tag
- Required
- Permitted content
- Flow content and optionally one
<figcaption>at the start or at the end
Example
#<figure>
<img
src="/collections/covers/kurs-html-css-od-podstaw.webp"
alt="An Egyptian temple wall divided like a page layout: a wide strip at the top, a large panel in the middle, and a row of smaller panels at the bottom"
width="300"
height="200"
>
<figcaption>Fig. 1. A page layout resembles a wall made of stone blocks.</figcaption>
</figure>Definition and usage
#The <figure> element wraps content that illustrates the main text but also makes sense on its own: a photo, diagram, chart, code listing, quote, or table. You add a caption with the <figcaption> element, as the first or last child.
A good test: could you move this illustration to the end of the chapter or to an appendix and the text would still make sense thanks to a reference like โsee figure 3โ? If so, it is a <figure>. An image that is part of a sentence, or an icon in a button, doesnโt need this element.
By default, browsers set <figure> in from the edges with a 40 pixel margin on the left and right. The <figcaption> caption becomes the accessible name of the whole illustration, but the image inside still needs its own alt.
Syntax
#<figure>
โฆ
<figcaption>Caption</figcaption>
</figure>Attributes
#This element has no attributes of its own. It supports the global attributes, such as class, id and hidden.
More examples
#<figure>
<figcaption>Listing 2. The simplest HTML document</figcaption>
<pre><code><!DOCTYPE html>
<html lang="en">
<head><title>My page</title></head>
<body>Hello!</body>
</html></code></pre>
</figure><figure>
<blockquote>
<p>The best time to learn programming was a year ago. The second best is today.</p>
</blockquote>
<figcaption>Ola, a JavaScript course graduate</figcaption>
</figure>Default CSS
#Most browsers display this element with these default styles:
figure {
display: block;
margin-block: 1em;
margin-inline: 40px;
}Browser support
#Widely available
Works in all modern browsers, phones included. You can use it without worry.
- Chrome
- Edge
- Firefox
- Safari
Good practices
#- A caption and
altplay different roles.altreplaces the image, while<figcaption>comments on it, so donโt repeat the same sentence in both. - The default 40 pixel side margin often breaks the layout on a phone. Reset it in CSS:
figure { margin: 0; }.
Related entries
#- <figcaption>The caption of an illustration inside a `<figure>` element.
- <img>An image embedded in the page.
- <picture>A container for several versions of an image. The browser picks the one that best fits the screen and format.
- <blockquote>A longer quotation from another source, displayed as a separate, indented block.
- <pre>Preformatted text: it preserves spaces, tabs, and line breaks exactly as they appear in the code.
Practise it in lessons
#Spotted a mistake or missing an example? Write to us.