Sub topic 12
Color and contrast for legibility
This chapter assumes you know how size, weight, and spacing separate headlines, subheads, and body text. It also assumes you can read basic CSS. Here you will use color as a supporting cue and measure whether readers can actually read the text.
Color supports hierarchy, it does not carry it
Size, weight, and spacing do most of the ranking work. Color adds a gentle second signal. A near-black headline over slightly softer body text, with lighter gray for captions and notes, tells the reader what to read first.
The risk is that each step lighter is also a step harder to read. Pale gray text looks refined in a mockup, but it fails for many readers, especially on low-quality screens or in bright light.
Measuring contrast
Contrast ratio compares the brightness of the text color with the brightness of its background. It runs from 1:1 (no difference) to 21:1 (black on white). The Web Content Accessibility Guidelines (WCAG) set minimum ratios:
| Text type | Level AA | Level AAA |
|---|---|---|
| Body text | 4.5:1 | 7:1 |
| Large text (24px, or 18.66px bold) | 3:1 | 4.5:1 |
Large text gets a lower minimum because its bigger strokes stay readable at lower contrast. This helps headlines, but body text and small captions still need 4.5:1.
Some reference points on a white background:
#333333is about 12.6:1.#767676is about 4.54:1, the lightest neutral gray that passes AA for body text.#999999is about 2.85:1, which fails.
Applying it to each level
The stylesheet below sets a different color for each level and keeps every pair above its threshold. The headline is darkest. Body text is slightly softer but still well above 4.5:1. Captions are the lightest level and still reach 7:1.
:root {
--bg: #ffffff;
--text-strong: #111111;
--text-body: #333333;
--text-muted: #595959;
}
body {
background: var(--bg);
color: var(--text-body);
}
h1,
h2 {
color: var(--text-strong);
}
.caption {
color: var(--text-muted);
font-size: 0.875rem;
}In this example, #595959 on white is about 7:1, so even the quietest level passes AAA. The ranking still reads clearly, because each level differs from the next in size and weight as well as color.
Checking a pair
Check every text and background pair in your layout. This includes text placed over photos, colored buttons, and text in dark mode, where a gray that passed on white may fail on charcoal. Use a contrast checker, or the color tools built into your browser's developer panel.
Common errors
- Using light gray for body text to make a page feel "airy".
- Checking contrast only for the headline, which is large and passes easily.
- Placing text on an image without a solid or darkened layer behind it.
- Using pure black on pure white for long reading, which some readers find harsh. A dark gray such as
#222222or#333333is gentler and still passes comfortably.
Exercise
A caption is set in 14px #999999 on a white background, a ratio of about 2.85:1. Does it pass WCAG AA? What is the smallest change that would fix it, and where does the new color rank among the colors in this chapter?
Show answer
It fails, because 14px text is not large text and needs 4.5:1. Darkening the color to #767676 (about 4.54:1) is the smallest fix among the grays listed here. It is lighter than #595959 and darker than #999999. A darker gray such as #595959 gives extra margin.