Keep the caption in the reading order when an image changes

A landscape card with its picture and visible caption outlined as one figure.
Illustration of a figure whose caption remains after the image in the source order.

A gallery card can keep its caption when you crop the photograph, remove the hover effect or switch to a narrow layout. Put the caption in the same HTML figure as the image. Then you can move the figure as a unit and style the words wherever you have room.

Compare two versions of the same card

In the first version, the visible words depend on a CSS rule such as .tile:hover::after { content: attr(data-caption); }. With that rule inactive, a sighted reader gets the picture alone. A data-caption attribute is application data, so use it for a script’s needs. Make the caption itself ordinary document content.

<div class="tile" data-caption="North stair, before the refit">
  <img src="stairs.jpg" alt="Narrow stone stairs with a brass rail">
</div>

In the second version, figcaption names the figure and stays after the image in the source. The HTML Standard allows the caption at the start or end of a figure. Here the order fits a gallery that introduces the visual and then gives its context.

<figure class="tile">
  <img src="stairs.jpg" alt="Narrow stone stairs with a brass rail">
  <figcaption>North stair, before the refit</figcaption>
</figure>

Read the caption with the picture disabled

Disable images or temporarily swap the file name for a missing one. Check that the caption still identifies the location and date context. Then read the alt text and caption in source order. The rail and material give information that the caption leaves out. If a replacement image is decorative or merely repeats the caption, the W3C decision tree points to empty alt text instead. If you switch to a detail photograph, update the alt to describe what readers can actually see in that crop.

At a narrow width, let the caption take a second line. If you add an overlay for larger screens, verify the text remains legible over both images. Put two unlike photographs through the same component and read their captions without hovering. That is the quickest way to spot a caption that still depends on the effect.

Sources