Sub topic 7
Choosing Colors That Meet Contrast Ratios
Prerequisites
Assume you can create wireframes, identify common controls, and read basic HTML and CSS. You should also know that a wireframe describes structure and behavior, not only visual style.
Why Contrast Matters
Contrast is the difference in brightness between foreground and background colors. Users need enough contrast to read text, identify controls, and distinguish interface boundaries. Low contrast creates barriers for people with low vision, color-vision differences, or temporary problems such as screen glare.
The Web Content Accessibility Guidelines (WCAG) define minimum contrast ratios. A ratio of 1:1 means the colors have no brightness difference. Black against white has a ratio of 21:1.
| Content | Minimum ratio |
|---|---|
| Normal-sized text | 4.5:1 |
| Large text | 3:1 |
| User-interface components and meaningful graphics | 3:1 |
Large text is at least 18 point, or 14 point bold, in WCAG terms. Do not use large text as an excuse to make important content difficult to read. Aim for stronger contrast when possible.
Contrast applies to more than body text. Check headings, links, placeholder text, button labels, borders that define controls, input outlines, icons, and chart marks. Disabled controls are treated differently by WCAG, but making them extremely faint can still confuse users.
Check Colors During Wireframing
Do not wait for the final color palette. In a wireframe, annotate the intended color roles:
text-primaryonsurface-default: at least 4.5:1text-largeonsurface-default: at least 3:1button-labelonbutton-primary: at least 4.5:1input-borderonsurface-default: at least 3:1focus-indicatoragainst adjacent colors: at least 3:1
A color picker or contrast checker can calculate a ratio. You can also calculate it with JavaScript:
function channel(value) {
const s = value / 255;
return s <= 0.03928
? 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(rgbA, rgbB) {
const [light, dark] = [
luminance(rgbA),
luminance(rgbB)
].sort((a, b) => b - a);
return (light + 0.05) / (dark + 0.05);
}
console.log(contrastRatio([0, 0, 0], [255, 255, 255]));The output is 21, which passes every listed threshold.
Common Errors
A pale gray label may look elegant but fail the normal-text threshold. A colored link may pass contrast while becoming indistinguishable from surrounding text. Add another cue, such as an underline or a distinct shape, when color communicates meaning.
Gradients, images, shadows, and patterned backgrounds can change contrast across the same text. Test the lowest-contrast area, or place text on a solid background. Check contrast in every state, including hover, focus, error, and selected states.
Exercise
A body-text color has a contrast ratio of 3.8:1 against its background. Does it meet the normal-text requirement, and what should you change?
Show answer
No. Normal-sized text needs at least 4.5:1. Choose a darker foreground, a lighter background, or both, then recheck every relevant state.