Sub topic 4
Contrast and accessible color choices
This chapter assumes you can write colors in hex, rgb(), and hsl(), and that you know how custom properties work. It covers how to check that your text and background colors are readable.
Why contrast matters
Contrast is the difference in brightness between a foreground color and its background. Low contrast is hard to read for people with low vision or color vision deficiency. It is also hard to read for anyone on a dim screen or in bright sunlight.
The Web Content Accessibility Guidelines (WCAG) measure contrast as a ratio from 1:1 (identical colors) to 21:1 (black on white). The ratio is computed from each color's relative luminance, which is a measure of how bright the color appears:
ratio = (L1 + 0.05) / (L2 + 0.05)Here L1 is the luminance of the lighter color and L2 is the luminance of the darker one. You will rarely compute this by hand. Browser developer tools and online checkers do it for you.
The WCAG thresholds
| Content | Level AA (minimum) | Level AAA (enhanced) |
|---|---|---|
| Normal text | 4.5:1 | 7:1 |
| Large text | 3:1 | 4.5:1 |
| UI components and graphics | 3:1 | no separate level |
Large text means at least 24px (18pt), or at least about 18.66px (14pt) when bold. Most projects target AA. Use AAA where you can, especially for long reading.
Gray text is a common failure. #777777 on white has a ratio of about 4.48:1 and fails AA. #767676 has about 4.54:1 and passes. A one-step change in the hex value decides the result, so check the actual numbers.
Checking colors in a stylesheet
Put the checked pairs in custom properties and note their ratios in comments. Anyone who changes a value then knows what to re-check.
:root {
--text: #1a1a1a; /* on --surface: about 17:1 */
--text-muted: #595959; /* on --surface: about 7:1 */
--surface: #ffffff;
--link: #0b5cad; /* on --surface: about 6.7:1 */
}
body {
color: var(--text);
background-color: var(--surface);
}
.hint {
color: var(--text-muted);
}Chrome and Firefox developer tools show the contrast ratio for a selected text color. Chrome marks passing and failing ratios with checkmarks in the color picker.
Common errors
- Checking only the main text. Placeholder text, disabled-looking labels, and captions also need enough contrast. Placeholder text is often left at the browser default or a pale gray.
- Ignoring transparency. A color such as
rgb(0 0 0 / 0.4)blends with whatever is behind it. Check the blended result, not the original color. - Text over images. The ratio changes across the image. Add a solid or semi-opaque overlay behind the text, and check the worst spot.
- Using color as the only signal. About 1 in 12 men has some form of color vision deficiency. A red error border with no other cue can go unnoticed.
- Removing focus outlines. Keyboard users need a visible focus indicator. Give it at least 3:1 contrast against the surrounding colors.
The next example avoids two of these errors. The link has an underline, so color is not its only cue, and the focus ring has strong contrast:
a {
color: #0b5cad;
text-decoration: underline;
}
a:focus-visible {
outline: 3px solid #0b5cad;
outline-offset: 2px;
}Respecting user preferences
Some users ask their operating system for more contrast. You can respond with the prefers-contrast media query:
@media (prefers-contrast: more) {
:root {
--text-muted: #1a1a1a;
}
}Dark themes need their own checks. A dark gray background with light gray text can fail just as easily as the light version. Test each theme separately.
A quick decision flow
Exercise
A page uses 16px regular text in #8a8a8a on a white background. The measured ratio is about 3.4:1. Does it meet WCAG AA, and what is the simplest fix?
Show answer
It fails. Normal text needs 4.5:1, and 3:1 applies only to large text (24px, or about 18.66px bold). The simplest fix is to darken the gray, for example to #767676 (about 4.54:1) or darker, and re-measure.