Sub topic 8
Checking contrast against WCAG
This chapter assumes you can write basic HTML and CSS, read hex and RGB colour values, and have built a palette with lightness scales like the one in the earlier chapters. You will learn how to measure whether two colours are readable together and how to pick pairs from your scales that pass.
Why contrast matters
Contrast is the difference in brightness between a foreground colour and its background. Low contrast makes text hard to read for people with low vision, for people on a dim phone screen in sunlight, and for anyone who is tired. The Web Content Accessibility Guidelines (WCAG) turn this into a number you can test, so you do not have to rely on your own eyes.
Your eyes are a poor judge here. Hue can make two colours look different even when their brightness is nearly the same. A blue and a red of equal brightness can look distinct but still be hard to read as text on each other.
The contrast ratio
WCAG 2 defines contrast as a ratio between 1:1 (identical colours) and 21:1 (black on white). The ratio uses relative luminance, a measure of how bright a colour appears, from 0 (black) to 1 (white).
The calculation has three steps:
- Convert each RGB channel from 0–255 to 0–1, then undo the gamma curve that screens apply.
- Combine the channels with weights that reflect how the eye responds: green counts most, blue least.
- Divide the lighter luminance plus 0.05 by the darker luminance plus 0.05.
function channel(value) {
const s = value / 255;
return s <= 0.04045 ? s / 12.92 : Math.pow((s + 0.055) / 1.055, 2.4);
}
function luminance([r, g, b]) {
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b);
}
function contrastRatio(colorA, colorB) {
const a = luminance(colorA);
const b = luminance(colorB);
const lighter = Math.max(a, b);
const darker = Math.min(a, b);
return (lighter + 0.05) / (darker + 0.05);
}
console.log(contrastRatio([0, 0, 0], [255, 255, 255])); // 21The function takes RGB arrays, so convert hex or HSL values first. Browser developer tools do this for you: inspect an element and the colour picker shows the ratio.
The thresholds
WCAG 2 sets minimum ratios at level AA, which is the usual target for websites. Level AAA is stricter.
| Content | AA minimum | AAA minimum |
|---|---|---|
| Normal text | 4.5:1 | 7:1 |
| Large text (about 24px, or 19px bold) | 3:1 | 4.5:1 |
| UI components and graphics (borders of inputs, icons) | 3:1 | not defined |
Aim for AA everywhere and use AAA for long body text where you can. Placeholder text, disabled-looking labels, and text over images are the places teams most often fail.
Using your scales to pass
The lightness steps you built earlier make this practical. Steps far apart on the same scale tend to have high contrast, and steps close together tend to have low contrast. A common approach is to fix the pairings by role:
- Body text:
--neutral-900on--neutral-50. - Muted text:
--neutral-700on--neutral-50. - Links:
--primary-700on--neutral-50. - Button label: white text on
--primary-500.
Test each pair rather than assuming. In the palette from this tutorial, --primary-500 (hsl(220 80% 50%)) is a mid-tone, so white text on it sits near the AA boundary for normal text. Check it before shipping, and if it fails, darken the button to --primary-700.
The accent needs the same care. A bright orange like --accent-500 is light enough that white text on it fails. Use dark text on the accent, or keep the accent for large shapes and decoration where no text sits on it.
Common errors
- Testing only the main states. Hover, focus, visited, and disabled states need their own checks.
- Ignoring borders. An input with a pale grey border on white can fail the 3:1 rule for UI components, and then users cannot see where the field is.
- Relying on colour alone. Contrast ratio does not cover colour-blind users. Error and success states should also use an icon or text, not only red and green.
- Checking light mode only. A pair that passes in light mode can fail after you reassign tokens for dark mode.
- Treating WCAG 2 as perfect. The formula can misjudge some pairs, especially light text on mid-tone colours. It is still the standard most audits and laws refer to, so meet it first.
Exercise
A paragraph uses the text colour [90, 90, 90] on a white background [255, 255, 255]. Using the contrastRatio function above, how would you decide whether it passes AA, and what would you do if the result were 4.3?
Show answer
Call contrastRatio([90, 90, 90], [255, 255, 255]) and compare the result with 4.5, because paragraph text is normal text. A result of 4.3 fails AA, since the ratio is not rounded up. Fix it by moving the text colour one step darker on the neutral scale (a lower lightness value), then measure again until the ratio is at least 4.5.
Exercise
Your input fields have a 1px border of --neutral-300 on a --neutral-50 page. Which WCAG threshold applies to that border, and why might it fail?
Show answer
The 3:1 threshold for UI components applies. Light neutral steps such as 300 and 50 are close in lightness, so their ratio is usually well below 3:1. Use a darker border step, such as --neutral-700, for form controls, and keep the lighter step for decorative dividers.