Figure, figcaption & image best practices
4 min lesson
- A <figure> groups an image with a caption, and <figcaption> holds the caption text.
- The caption becomes officially connected to the image, which helps readers and screen readers.
- Image best practices: pick the right format, keep file sizes small, always give alt text, and set width and height to avoid layout jumps.
- Common formats are JPEG for photos, PNG for sharp graphics, SVG for logos and WebP for small modern images.
- This builds on Links & images, where you learned src and alt, and adds structure and quality rules.
- The example wraps an image in a figure with a caption; you will see the picture and its caption underneath.
- This completes the Content group; you can now build a full page of text, lists, links, images and captions.
- Wrap an image and its caption in figure with figcaption.
- Always set alt, and add width and height to prevent layout shifts.
- Choose the smallest format that still looks good.
- Compress images before uploading; large files slow the whole page.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Figure & figcaption</title>
</head>
<body>
<!-- figure groups the image with its caption -->
<figure>
<!-- width and height keep the layout stable while loading -->
<img src="mountains.jpg" alt="Snowy mountains at sunrise" width="480" height="320">
<!-- The caption describes the image for the reader -->
<figcaption>Sunrise over the mountains, taken from the north ridge.</figcaption>
</figure>
</body>
</html>What happens: A page showing the image of snowy mountains at sunrise (480 by 320 pixels) followed by the caption "Sunrise over the mountains, taken from the north ridge."
Breaking it down
figure
<figure>...</figure>Groups an image with its caption so they stay together. It can hold other media too, like code or diagrams.
img with size
<img src="..." width="480" height="320">Sets the display size in pixels. The browser reserves space before the image loads, so the layout does not jump.
figcaption
<figcaption>Sunrise over the mountains...</figcaption>Adds a visible caption tied to the figure. Screen readers announce it as the image's caption.
Image formats
| Format | Best for |
|---|---|
| JPEG | Photographs with many colors |
| PNG | Sharp graphics and transparency |
| SVG | Logos and icons that scale |
| WebP | Small modern images for the web |
A helpful analogy
A figure is like a photo album page: the picture and its handwritten note live together on one page. figcaption is that note, permanently attached to the photo.
Nesting rules
- 1.figcaption must be the first or last child of figure, not in the middle.
- 2.Use one figcaption per figure; wrap multiple images in one figure only when the caption describes them all.
Common mistakes
Putting the caption in a plain paragraph
Avoid
<img src="a.jpg" alt="A"><p>Caption</p>Use instead
<figure><img src="a.jpg" alt="A"><figcaption>Caption</figcaption></figure>A paragraph caption is not linked to the image. Screen readers will not know they belong together.
Uploading huge image files
Avoid
A 5 MB photo at 4000 pixels wide shown at 480 pixelsUse instead
A compressed photo near 480 pixels wideThe browser downloads the full file even if it displays it small. Large files slow the page for everyone.
Quick tips
- •Resize and compress images before adding them to the project; tools like Squoosh do this in the browser.
- •Set width and height on every img so the page does not jump while loading.