Emmanuel Akinfulubi

HTML and CSSHTML and CSS

Add an image caption with figure and figcaption in HTML

5 A figure with a caption

easy

Give an image a visible caption that stays attached to it. The caption and the alternative text should say different things.

Stuck? Three hints, smallest first
  1. 1. <figure> wraps the image; <figcaption> goes inside it, first or last.
  2. 2. The caption is for everyone. The alt is for someone who cannot see the image.
  3. 3. Repeating the caption in the alt means a screen reader hears the same sentence twice.

index.html

<figure class="fig">
  <img
    width="240" height="140"
    alt="A queue of about thirty people along a painted kerb"
    src="data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='140'%3E%3Crect width='240' height='140' fill='%23e9ecf6'/%3E%3Crect x='16' y='108' width='208' height='10' fill='%233055d8'/%3E%3C/svg%3E">
  <figcaption>Boarding for the 06:30 to Abuja. Photographed in March.</figcaption>
</figure>

style.css

.fig {
  max-width: 240px;
  margin: 0;
  padding: 8px;
  border: 1px solid #c9cad1;
  border-radius: 8px;
  background: #f7f7f8;
}

.fig img {
  display: block;
  width: 100%;
  height: auto;
  border-radius: 4px;
}

.fig figcaption {
  margin-top: 8px;
  color: #5c5c66;
  font-size: 0.85rem;
  line-height: 1.45;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 5

// Compare the two strings rather than trusting that they differ.
const alt = (/alt="([^"]*)"/.exec(html) || [, ''])[1].trim();
const caption = (/<figcaption>([\s\S]*?)<\/figcaption>/.exec(html) || [, ''])[1].trim();

test('a figure wraps the image', hasTag(html, 'figure'), true);
test('and carries a caption', hasTag(html, 'figcaption'), true);
test('the caption is inside the figure', /<figure[\s\S]*<figcaption[\s\S]*<\/figure>/.test(html), true);
test('the alt text is not a copy of the caption', same(alt, caption), false);
test('both actually say something', alt.length > 10 && caption.length > 10, true);
test('the figure margin is reset, since browsers indent it', /\.fig\s*\{[^}]*margin\s*:\s*0/.test(css), true);
test('the caption is styled as secondary text', /figcaption\s*\{[^}]*color\s*:\s*#5c5c66/.test(css), true);
test('the image still declares its intrinsic size', hasAttr(html, 'width') && hasAttr(html, 'height'), true);

Output


      

The fourth check is the teaching. A caption describes what the picture means in the flow of the page; alt describes what is in it. Paste one into the other and a screen reader reads the same sentence twice, once as the image and once as the caption. Note also that <figure> arrives with a browser default margin of about 40px on each side, so resetting it is nearly always step one.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.