Course contentsShow
← Web Development
Lesson 56 of 2,9822. HTML FundamentalsFree lesson

The <figure> and <figcaption> Elements

Grouping media content with captions using <figure> and <figcaption> for self-contained illustrations.

The <figure> and <figcaption> Elements

What you'll learn: How to semantically group images, diagrams, or media with their captions in HTML.

What Are These Elements?

Think of <figure> like a picture frame in a museum that contains both the artwork and the little placard describing it. In HTML, <figure> wraps self-contained content (usually an image, illustration, or diagram), while <figcaption> provides the caption or description for that content.

These elements tell browsers and screen readers: "This content and its caption are a single, self-contained unit that relates to the main document but could be moved elsewhere without breaking the flow."

Why Use Them?

Without semantic HTML, you might just put an image and some text next to each other. But <figure> and <figcaption> create a meaningful relationship between the two. Screen readers can announce the caption as belonging to the image, and you've clearly indicated the content's purpose in your document structure.

Basic Structure

A <figure> wraps the content, and <figcaption> (optional, but recommended) describes it:

<figure>
  <img src="sunset.jpg" alt="Golden sunset over mountains">
  <figcaption>A stunning sunset captured in the Rocky Mountains, July 2024.</figcaption>
</figure>

The <figcaption> can appear before or after the image, though it typically comes after.

When to Use It

Use <figure> for self-contained content like photos, illustrations, diagrams, code snippets, or even quotes that are referenced from the main content but aren't strictly part of the text flow.

Key Takeaway: The <figure> and <figcaption> elements semantically group media content with its caption, creating accessible, self-contained units that enhance document structure and meaning.