Sub topic 15
Accessibility: contrast checks and alternative text
This chapter assumes you know how to place text over an image with a scrim (see the earlier chapters on contrast, legibility and overlays). It also assumes basic HTML and CSS. Here you turn "looks readable to me" into checks you can measure, and you make sure images work for people who cannot see them.
Contrast ratios
A contrast ratio compares the brightness of two colors. It runs from 1:1 (identical colors) to 21:1 (black on white). The Web Content Accessibility Guidelines (WCAG) set these minimums for level AA:
| Content | Minimum ratio |
|---|---|
| Body text (under 24 px regular, or under about 19 px bold) | 4.5:1 |
| Large text (24 px and up, or 19 px bold and up) | 3:1 |
| Meaningful graphics and interface parts | 3:1 |
Level AAA asks for 7:1 for body text and 4.5:1 for large text. Use it for long reading where you can.
Checking text over a photo
A photo has many colors, so one ratio is not enough. Check the worst case: the lightest patch of image that sits directly behind your text, if the text is white. If the text is dark, check the darkest patch.
- Pick the text color and the background patch with an eyedropper.
- Enter both into a contrast checker, such as the one built into Figma plugins or a browser's developer tools.
- If the ratio is under the minimum, strengthen the scrim, move the text to a calmer area, or change the text color.
A gradient scrim helps because it makes the area behind the text predictable. The hero example from the overlay chapter does this: the scrim is darkest at the bottom, where the text sits.
.hero {
position: relative;
min-height: 24rem;
display: flex;
flex-direction: column;
justify-content: flex-end;
padding: 2rem;
color: #fff;
background:
linear-gradient(to top, rgba(0, 0, 0, 0.7), rgba(0, 0, 0, 0) 60%),
url("coast.jpg") center / cover no-repeat;
}White text on a 70% black layer over even a pure white photo gives a patch of about #4D4D4D. That patch has a contrast ratio of roughly 8.4:1 against white, which passes comfortably. Check the text's actual position on the gradient, because the scrim fades to zero at the 60% mark.
Alternative text
Alternative text (alt text) is a written description of an image. Screen readers read it aloud, and browsers show it when the image fails to load. Write it by asking what the image contributes to the page.
<figure>
<img src="harbor.jpg" alt="Fishing boats moored in a harbor at dawn">
<figcaption>Harbor at dawn, 6:10 a.m.</figcaption>
</figure>Follow these rules:
- Describe what matters in context, not every detail. Keep it short, usually under about 125 characters.
- Do not start with "image of" or "photo of". Screen readers already announce an image.
- Do not repeat the caption word for word. A reader would hear it twice.
- If the image is purely decorative, such as a background texture, use an empty
alt=""so screen readers skip it. - If text is part of the image, repeat that text in the alt text.
Common errors
- Checking contrast against the image's average color instead of the area behind the text.
- Using light gray text because it "looks refined". Gray on white often falls below 4.5:1.
- Leaving out
altentirely. Screen readers may then read the file name. - Putting key information only in an image, with no text version.
Exercise
White text sits on a photo. The lightest patch behind it is a pale sky, and the contrast ratio there is 2.8:1. The text is 18 px regular. Name two ways to fix it.
Show answer
Body text needs 4.5:1, so 2.8:1 fails. You can darken the area behind the text with a stronger scrim or gradient, or move the text to a darker part of the image. Changing the text color to a dark one that measures at least 4.5:1 against the sky also works.
Exercise
A decorative wave pattern runs behind a heading. What should its alt text be, and why?
Show answer
Use an empty alt attribute (alt=""). The pattern adds no information, so screen readers should skip it instead of reading a pointless description.