<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
On this page

Example

#
HTML
<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>
Preview

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

#
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

#
A code listing with a caption
HTML
<figure>
  <figcaption>Listing 2. The simplest HTML document</figcaption>
  <pre><code>&lt;!DOCTYPE html&gt;
&lt;html lang="en"&gt;
  &lt;head&gt;&lt;title&gt;My page&lt;/title&gt;&lt;/head&gt;
  &lt;body&gt;Hello!&lt;/body&gt;
&lt;/html&gt;</code></pre>
</figure>
Preview
A quote with an author
HTML
<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>
Preview

Default CSS

#

Most browsers display this element with these default styles:

CSS
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 alt play different roles. alt replaces 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

#

Practise it in lessons

#

Spotted a mistake or missing an example? Write to us.