HTML5
Explain the Use of the Figure Tag in HTML5
The HTML5 <figure> element represents self-contained content — such as an image, diagram, chart, or code snippet — that is referenced from the main text but could stand on its own. Its optional child, <figcaption>, provides a caption or legend for that content.
What Does <figure> Do?
<figure> semantically groups a piece of media with its caption so browsers, search engines, and assistive technologies understand they belong together. Unlike a plain <img> followed by a <p>, the association between the image and its caption is explicit and machine-readable.
Runnable Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Figure Demo</title>
<style>
figure { border: 1px solid #ddd; padding: 8px; width: 320px; }
figcaption { font-size: 14px; color: #555; text-align: center; margin-top: 6px; }
</style>
</head>
<body>
<figure>
<img src="https://www.w3schools.com/html/img_girl.jpg"
alt="A girl standing outdoors" width="300">
<figcaption>Fig 1. A sample photograph with a caption.</figcaption>
</figure>
</body>
</html>The <figcaption> can be placed as the FIRST or LAST child of <figure>, but not in the middle. A <figure> may contain more than one image, a table, or a code block — not just a single image.
figure vs a plain image + paragraph
| Approach | Semantic association? | Recommended? |
|---|---|---|
| <img> then <p>caption</p> | No explicit link | No |
| <figure> + <figcaption> | Yes, machine-readable | Yes |
Key Takeaways
- <figure> groups self-contained media that is referenced from the text.
- <figcaption> gives it a caption and must be the first or last child.
- It improves semantics, accessibility, and SEO over loose markup.
- Figures can hold images, charts, tables, or code — not only images.
