Full answer
Contrast is the difference in brightness between text and its background. Low contrast is hard to read for people with low vision, for people in bright light and for many others. The Web Content Accessibility Guidelines (WCAG) set minimum ratios, measured from 1:1 (no contrast) to 21:1 (black on white).
Common level AA targets:
- Normal text: at least 4.5:1.
- Large text (about 24px, or 19px and bold): at least 3:1.
- User interface parts and graphics that carry meaning, such as an input border, a focus ring or an icon: at least 3:1 against what is next to them.
- Logos and purely decorative items are exempt. Disabled controls are also exempt, although a very faint look is still unkind.
The ratio uses the relative luminance of the two colours: (L1 + 0.05) / (L2 + 0.05), where L1 is the lighter. Luminance comes from the sRGB values after converting each channel to a linear value and weighting the green channel most.
Practical points:
- Check all states: hover, focus, visited, disabled placeholder and error text.
- Text over images and gradients needs a check at the worst spot, or a solid overlay behind the text.
- Do not use colour alone to give meaning. Add text, an icon or a pattern as well.
- With a dark theme, check every pair again.
- Browser DevTools show the ratio in the colour picker, and automated tools find many problems, but they cannot judge everything, so also look at the page by eye.
Black on white gives 21.00. Grey #767676 on white gives 4.54, which passes for normal text. The slightly lighter #777777 gives 4.48, which fails by a very small amount.
Use the result as a guide, and test the real page, because anti-aliasing and font weight also affect how readable text is.
<p id="out"></p>
<script>
const lin = c => { c /= 255; return c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4; };
const lum = ([r, g, b]) => 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);
function ratio(a, b) {
const [hi, lo] = [lum(a), lum(b)].sort((x, y) => y - x);
return (hi + 0.05) / (lo + 0.05);
}
window.r1 = ratio([0, 0, 0], [255, 255, 255]);
window.r2 = ratio([0x76, 0x76, 0x76], [255, 255, 255]);
window.r3 = ratio([0x77, 0x77, 0x77], [255, 255, 255]);
out.textContent = [r1, r2, r3].map(x => x.toFixed(2)).join(" / ");
</script>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Normal text needs 4.5:1; large text needs 3:1.
- UI parts and meaningful graphics need 3:1.
- The ratio is (L1 + 0.05) / (L2 + 0.05).
- Check hover, focus, error and dark theme states.
- Do not use colour alone to show meaning.
Common mistakes
- Using light grey text on white for hints and placeholders.
- Checking only the default state and not hover, focus or disabled states.
- Placing text on a photo with no overlay and checking at the easiest spot.
Follow-ups
- How do you check text on a gradient?
- What contrast does a focus ring need?
Tips
- Give the three numbers: 4.5, 3 and 3.
- Say that colour alone must not carry meaning.