HTML Tutorial / Group Images and Captions with figure and figcaption

figure groups self-contained content such as an image, chart, or code sample, and figcaption gives that figure a visible caption. Not every image needs these elements. Use them when the content and its explanation belong together as one unit.

Keep an image and caption together

A travel photo and its location or context can form one figure. Put figcaption as the first or last child of figure. Use the same downloadable example image and path setup as Images: img, Alt Text, and Dimensions.

<figure>
  <img src="html-image-sample-lake-tree-960-v1.webp"
       alt="A tree on a small island in a lake with mountains beyond"
       width="960" height="540">
  <figcaption>Figure: the lake-and-tree example used for this lesson</figcaption>
</figure>

If the picture exceeds the content width, put this CSS in head's style element. Editing the caption does not automatically change alt, so review each independently.

figure { margin: 1rem 0; }
figure img { max-width: 100%; height: auto; }

Place the 960px example from the image lesson beside the HTML file, then put this code inside body. The filename is html-image-sample-lake-tree-960-v1.webp and its actual size is 960×540 pixels. The caption appears below; move figcaption to the first-child position to place it above.

Alt text and captions do different jobs

The alt text conveys the image's information or function when the image cannot be perceived. The visible figcaption supplies a title, source, or context for everyone. Repeating exactly the same sentence in both can create redundant reading. In this example, alt describes the visible information and the caption identifies its role as a lesson example. Review the image's function alongside keyboard use in Accessibility Basics: Headings, Links, Images, and Keyboard.

For a purely decorative image, consider alt="", but first check that important information is available elsewhere. A chart needs its main trend or result conveyed, not merely the phrase “bar chart.” If its data requires a longer explanation, provide that in nearby text or a table and let the alt text lead readers to it. The W3C WAI alt-text decision tree helps with this choice.

When can you omit figure?

An ordinary image within the flow of a paragraph does not need figure if it has no separate caption or reference. A diagram discussed as “the following figure,” or a code sample with a caption, is a stronger candidate. A figure can contain more than an image. See the MDN figure reference for its content model.

To link a source in the caption, replace the preceding figcaption as follows. This link leads to the lesson supplying the actual example file. For a different image, identify its real creator/source and check its usage terms; attribution alone does not grant permission.

<figcaption>Example image source: <a href="https://www.codingfactory.net/en/15491">the file supplied with the images lesson</a>.</figcaption>

The source text and link appear below the picture, while alt continues to describe the tree, lake, and mountains. Do not repeat every attribution detail in alt.

If the result looks wrong

Browsers may add default margins around figure. Check its CSS margin before changing the HTML structure. If the image is missing, check the filename and relative path. If the caption is missing, verify that figcaption is inside figure and the closing tags are present. A caption does not automatically provide appropriate alt text; review both separately.