Sub topic 6
Captions and Other Secondary Text
This chapter assumes you know basic HTML elements and CSS selectors, and that you have read the earlier chapters on semantic structure, the type scale, headings, and body text. Here you will mark up and style captions and similar secondary text so that it reads as supporting information and stays tied to the content it describes.
What counts as secondary text
Secondary text supports the main content. Readers can skip it and still follow the page, but it is useful when they want more detail. Common examples:
- Captions for images, charts, tables, and code samples.
- Help text under form fields, such as "Use at least 12 characters."
- Metadata such as bylines, publication dates, and reading times.
- Legal notes, footnotes, and "fine print" next to a price or button.
- Timestamps and counters in lists or comment threads.
All of these sit below body text in the hierarchy. Two mistakes are common. The first is to make secondary text so small or so pale that people cannot read it. The second is to style it so close to body text that readers cannot tell the two apart. This chapter helps you avoid both.
Marking up captions with figure and figcaption
A caption describes a specific piece of content. The HTML element for this is <figcaption>, which must be a child of a <figure>. The <figure> element wraps self-contained content, such as an image, diagram, table, or code listing, together with its caption.
<figure>
<img src="boots.jpg" alt="A pair of leather hiking boots on a rock">
<figcaption>Leather boots after a week on the trail.</figcaption>
</figure>The figure element creates a programmatic link between the content and its caption. Assistive technology can announce that the image has a caption, and the relationship is clear in the document structure, not only in the visual layout.
A figcaption can be the first or last child of its figure. Place it where your design needs it. Below the content is the usual choice for images, and above is common for tables.
Caption versus alt text
The alt attribute and the caption do different jobs, and one does not replace the other.
| Row | `alt` attribute | `figcaption` |
|---|---|---|
| Visible to sighted users | No (only if the image fails to load) | Yes |
| Read by screen readers | Yes, in place of the image | Yes, as text |
| Purpose | Stands in for the image | Adds context, credit, or explanation |
| Typical length | A short phrase | One or two sentences |
Avoid repeating the same sentence in both. If the caption already says everything the image shows, a screen reader user hears the same words twice. Let the alt text describe what the image looks like, and let the caption say why it is there or where it came from.
<figure>
<img src="river.jpg" alt="A wide river with a wooden footbridge in morning mist">
<figcaption>The Alder Creek crossing, 6 km from the trailhead. Photo: M. Ortiz.</figcaption>
</figure>If an image is purely decorative, use alt="" and do not give it a caption. A caption on a decorative image gives it importance it does not have.
Styling captions
Captions usually get a smaller size and a softer color than body text. Use a size from your type scale, not a one-off value. The earlier chapters defined --text-sm for this purpose.
:root {
--text-sm: 0.875rem;
--text-base: 1rem;
--leading-body: 1.5;
}
figcaption {
font-size: var(--text-sm);
line-height: var(--leading-body);
color: #595959;
margin-top: 0.5rem;
max-width: 65ch;
}Each declaration has a reason:
font-size: var(--text-sm)sets the caption one step below body text. At 0.875rem the text is 14px at the default browser setting, which is small but still readable.line-heightstays generous. Small text needs as much line spacing as body text, because the lines are harder to track. Do not tighten it as you would for a heading.color: #595959is a dark gray. On a white background it has a contrast ratio of about 7:1, which passes WCAG AA for normal text (4.5:1) and also the stricter AAA level (7:1). Lighter grays such as#999fail AA on white.margin-topkeeps the caption close to its content. Spacing is part of the grouping signal, discussed below.max-widthkeeps captions from stretching wider than the image or the body column.
Matching the caption width to the image
A caption that is much wider than its image looks detached. When the figure is shrink-wrapped to the image, the caption wraps at the image width. One way to do this is to make the figure an inline-sized block:
figure {
margin: 1.5rem 0;
width: fit-content;
max-width: 100%;
}
figure img {
display: block;
max-width: 100%;
height: auto;
}With width: fit-content, the figure is as wide as its widest child, which is normally the image. The caption wraps to that width. The max-width: 100% rule lets the figure shrink on narrow screens, and height: auto on the image keeps its proportions.
Browser default margins on figure
Browsers give <figure> a default margin, commonly 1em top and bottom and 40px left and right. The side margins surprise many developers, because the figure ends up indented relative to the paragraphs around it. Set margin explicitly, as shown above, so the layout does not depend on the user agent stylesheet.
Grouping through proximity
Readers decide which text belongs to which content mostly by distance. A caption that sits 0.5rem below its image and 2rem above the next paragraph reads as part of the image. If the gaps are equal, the caption floats between the two and its target is unclear.
Apply this rule: the space between a caption and the thing it describes should be clearly smaller than the space between the caption and whatever follows. For figures, put the larger margin on the figure itself, and the small margin inside it.
figure {
margin: 2rem 0;
}
figcaption {
margin-top: 0.5rem;
}Here the outer margin is four times the inner one. That ratio is enough to make the grouping obvious without any border or background.
Other secondary text
Help text for form fields
Help text explains how to fill in a field. Connect it to the control with aria-describedby so that a screen reader reads it when the field gets focus. A visual position under the field is not enough on its own.
<label for="password">Password</label>
<input id="password" name="password" type="password"
autocomplete="new-password" aria-describedby="password-hint">
<p id="password-hint" class="hint">Use at least 12 characters.</p>.hint {
font-size: var(--text-sm);
color: #595959;
margin: 0.25rem 0 1rem;
}The label stays prominent and the hint stays quiet. The label chapter covers labels in more detail.
Metadata with small, time, and similar elements
For bylines and dates, choose the element that matches the content. The <time> element holds a date or time and can carry a machine-readable datetime value. The <small> element is for side comments and fine print, such as copyright lines and legal notes. It is not a general-purpose "make this smaller" tag.
<p class="byline">
By Priya Nair, <time datetime="2026-11-20">20 November 2026</time>
</p>
<p class="legal"><small>Prices include tax. Offer ends 30 November.</small></p>.byline {
font-size: var(--text-sm);
color: #595959;
}
small {
font-size: var(--text-sm);
}Browsers already render <small> in a smaller size, but setting font-size from your scale makes the result consistent and avoids the text getting smaller each time small is nested.
Footnotes and asides
Content that is related but not essential can go in an <aside>. Footnote markers are usually links that point to a note further down the page. Style the notes like captions, and make sure the marker has enough hit area to tap on a phone.
Emphasis within secondary text
Secondary text can still need emphasis. A caption may include a photo credit or a warning. Use <em> for stress emphasis and <strong> for strong importance, and keep the number of such spots low. If everything in a caption is bold, nothing stands out. For credits, a common approach is to separate the credit from the description with a space and a lighter or different style, not a bigger one.
Common mistakes
- Using a paragraph for a caption. A
<p>below an image looks the same, but nothing links the two. Usefigureandfigcaption. - Using
figcaptionoutside afigure. This is invalid HTML. Browsers will still render it, but assistive technology gets no relationship. - Light gray text. Many designs use
#aaaor#bbbfor secondary text. On white these give ratios of about 2.3:1 and 1.9:1, well below the 4.5:1 minimum. - Pixel font sizes. A caption set with
font-size: 12pxignores a user's browser font-size preference in some cases and breaks the scale. Useremvalues from your tokens. - Going below about 0.75rem. Even with good contrast, text under 12px is difficult to read for many people. For most pages, 0.875rem is a practical floor.
- Using
smallas a styling tool. If the text is not a side comment or fine print, use a class and a CSS rule instead. - Equal spacing around the caption. As shown above, this hides which content the caption belongs to.
How secondary text fits into the hierarchy
The diagram shows the relative levels you have built so far, from the strongest signal to the weakest. Secondary text sits at the bottom, but it is still part of the structure.
Labels sit at a level close to body text because users must read them to complete a task. Captions and hints sit below because they supplement, and they are never the only place where critical information appears.
A worked example
This example brings the pieces together: a figure with a caption, a byline, and a form field with a hint.
<article>
<h1>Crossing Alder Creek</h1>
<p class="byline">By Priya Nair, <time datetime="2026-11-20">20 November 2026</time></p>
<figure>
<img src="river.jpg" alt="A wide river with a wooden footbridge in morning mist">
<figcaption>The Alder Creek crossing, 6 km from the trailhead. Photo: M. Ortiz.</figcaption>
</figure>
<p>The bridge is the only safe crossing between May and September.</p>
<form>
<label for="trip-email">Email for trail alerts</label>
<input id="trip-email" name="email" type="email"
autocomplete="email" aria-describedby="trip-hint">
<p id="trip-hint" class="hint">We send one message per closure. No other mail.</p>
</form>
</article>:root {
--text-sm: 0.875rem;
--text-base: 1rem;
--leading-body: 1.5;
--muted: #595959;
}
.byline,
.hint,
figcaption {
font-size: var(--text-sm);
line-height: var(--leading-body);
color: var(--muted);
}
figure {
margin: 2rem 0;
width: fit-content;
max-width: 100%;
}
figure img {
display: block;
max-width: 100%;
height: auto;
}
figcaption {
margin-top: 0.5rem;
}
.hint {
margin: 0.25rem 0 1rem;
}Grouping the three selectors in one rule means every kind of secondary text shares one size and color. When you change --muted, the byline, hint, and caption change together. That is the main benefit of treating secondary text as one role with one definition.
Testing secondary text
Run these checks on each page:
- Zoom the browser text to 200%. Captions should wrap and stay readable, with no clipping.
- Check the contrast ratio of your muted color against the actual background, including any card or banner background.
- Use a screen reader on one figure and one form field. Confirm that the caption is read with the image and the hint is read when the input gets focus.
- Blur your eyes or squint at the page. Captions should visibly belong to their images, and secondary text should look quieter than body text.
Exercise
A page has an image followed by <p class="caption">Sunset over the bay</p>, styled with color: #aaa; font-size: 11px;. Name three problems and give a fix for each.
Show answer
(1) The caption is not tied to the image. Wrap both in <figure> and use <figcaption>. (2) #aaa on white has a contrast ratio of about 2.3:1, below the 4.5:1 minimum. Use a darker gray such as #595959. (3) 11px is a fixed pixel size below a comfortable minimum and ignores the type scale. Use font-size: var(--text-sm), which is 0.875rem.
Exercise
You have a figure with 2rem of space above the next paragraph and a caption with 2rem of space above it, between the image and the caption. Readers say they cannot tell which image the caption belongs to. What do you change?
Show answer
Reduce the space between the image and the caption to a small value such as 0.5rem, and keep the larger space (2rem) below the figure. The caption is then closer to its image than to the next paragraph, so proximity groups them.