Sub topic 7
Choosing colors that meet contrast ratios
This chapter assumes you know the basics of WCAG levels (A, AA, AAA) and can read simple HTML and CSS. It covers how contrast is measured, which thresholds apply to which elements, and how to check a color pair while the design is still a wireframe.
Why contrast belongs in the wireframe
Wireframes are often grayscale, so contrast can look like a visual-design problem for later. In practice, the decisions that cause failures are made early: light gray placeholder text, thin outlined buttons, pale borders around inputs, and gray "disabled-looking" secondary links. If the wireframe uses a pale gray for hints, that gray usually survives into the final palette.
Choosing a small palette of tested pairs at this stage means later visual work starts from colors that already pass.
What WCAG measures
The WCAG contrast ratio compares the relative luminance of two colors. Relative luminance is a number from 0 (black) to 1 (white) that approximates how bright a color appears to the eye. The ratio is:
(L1 + 0.05) / (L2 + 0.05)L1 is the lighter color's luminance and L2 is the darker one's. The result ranges from 1:1 (identical colors) to 21:1 (black on white).
Two points often surprise people:
- Hue does not matter on its own. A saturated red and a saturated green can have nearly the same luminance, so the ratio between them is close to 1:1 even though they look different to many people.
- The ratio is symmetric. Dark text on a light background and light text on the same dark background give the same number, although they may not read equally well.
The thresholds
WCAG 2.2 sets these minimums for level AA, and higher ones for AAA.
| Content | Success criterion | AA | AAA |
|---|---|---|---|
| Normal text | 1.4.3 Contrast (Minimum) | 4.5:1 | 7:1 |
| Large text | 1.4.3 / 1.4.6 | 3:1 | 4.5:1 |
| UI components and graphical objects | 1.4.11 Non-text Contrast | 3:1 | no AAA criterion |
Large text means at least 18 point (24 CSS pixels) regular weight, or at least 14 point (about 18.66 CSS pixels) bold. Anything smaller is normal text.
Non-text contrast (1.4.11) applies to two things:
- The visual information needed to identify a user interface component and its states. Examples are an input's border, a checkbox outline, and the filled part of a toggle.
- Graphical objects needed to understand the content, such as chart lines and meaningful icons.
Several things are exempt from the ratios:
- Inactive (disabled) components.
- Pure decoration.
- Text that is part of a logo or brand name.
- Incidental text, such as text in a photo that is not the subject.
Disabled controls are exempt, but a design that makes everything look disabled is still hard to use. Keep the exemption for controls that are actually disabled.
Which pairs to check
For each wireframe, list every foreground and background pair that carries meaning. A useful checklist:
- Body text on the page background.
- Secondary text, such as captions, hints, timestamps and placeholder text.
- Link text against surrounding text and against the background.
- Button label on button fill.
- Button, input and checkbox borders against the background they sit on.
- Text on any colored surface: banners, alerts, badges, tags.
- Icons that carry meaning without a text label.
- Chart lines, bars and legend swatches.
- The focus indicator against adjacent colors (see the chapter on focus visibility).
- Each interactive state: hover, active, selected, error.
Check each state separately. A button that passes at rest can fail when hover darkens its fill but the label stays mid-gray.
Computing a ratio
Design tools and browser dev tools report contrast, but it helps to know what they calculate. The function below follows the WCAG definition. The channel conversion removes the gamma encoding of sRGB, and the weights reflect how strongly the eye responds to green, red and blue.
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);
}
// Black on white gives 21. Normal text needs at least 4.5.
console.log(contrastRatio([0, 0, 0], [255, 255, 255]));Try a few common grays against white to calibrate your eye:
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);
}
const white = [255, 255, 255];
for (const gray of [153, 118, 100, 85]) {
const ratio = contrastRatio([gray, gray, gray], white);
console.log(`#${gray.toString(16).repeat(3)} on white: ${ratio.toFixed(2)}`);
}The output shows that #999999 (about 2.8:1) fails for text and is also below 3:1 for borders. #767676 (about 4.5:1) is the lightest pure gray that passes AA for normal text on white. This is why a "light gray hint" in a wireframe is risky.
Common errors
Rounding up. WCAG does not round. A ratio of 4.49:1 fails the 4.5:1 requirement. Do not let a tool's rounded display convince you that a borderline pair passes.
Checking only text. Teams often fix labels and forget input borders. A white input on a white page with a #cccccc border has a ratio near 1.6:1, so the field's boundary is nearly invisible to a user with low vision.
Placeholder text as the label. Placeholder text is normal text and must meet 4.5:1. Because most placeholder styling is pale gray, it usually fails. Placeholders also disappear on input, so use a persistent label instead (covered in the forms chapter).
Ignoring text over images and gradients. Contrast must hold wherever the text actually sits. Measure against the worst part of the background, or place the text on a solid or semi-opaque panel.
Testing one theme. If the design offers dark mode or a high-contrast variant, each theme needs its own check. A color that passes on white may fail on charcoal.
Trusting a brand color. Brand colors are often mid-tones. They may work as large decorative fills but fail as text or as a button label color. Agree on a text-safe variant of the brand color early.
Documenting a tested palette
In the wireframe, replace vague notes such as "gray text" with named tokens and their verified ratios. A short table that travels with the wireframe is enough.
| Token | Foreground | Background | Ratio | Use |
|---|---|---|---|---|
| text-primary | #1a1a1a | #ffffff | 17.4:1 | Body text |
| text-secondary | #595959 | #ffffff | 7.0:1 | Hints, captions |
| border-input | #767676 | #ffffff | 4.5:1 | Input and checkbox borders |
| action-label | #ffffff | #0b5cad | 6.9:1 | Primary button label |
Developers can then implement the tokens as CSS custom properties, and a later change to a token triggers a re-check in one place.
:root {
--text-primary: #1a1a1a;
--text-secondary: #595959;
--border-input: #767676;
--action-fill: #0b5cad;
--action-label: #ffffff;
}Checking in a low-fidelity design
You do not need final colors to catch problems. A practical routine:
- Fill the wireframe's grays with the intended hex values rather than "light" and "dark".
- Run each pair from the checklist through a contrast checker or the function above.
- Record the ratio next to the element in your annotations.
- View the wireframe in grayscale and at reduced screen brightness. Elements that vanish are likely to fail.
Exercise
A form wireframe uses #999999 text for hints on a white background, and a #cccccc 1px border around text inputs on white. Which WCAG criteria do these fail, and what is a minimal fix for each?
Show answer
The hint text is normal text at about 2.8:1, so it fails 1.4.3 (needs 4.5:1). Darken it to at least #767676, or better #595959 for a margin. The input border is a UI component boundary at about 1.6:1, so it fails 1.4.11 (needs 3:1). Darken it to at least #949494 (about 3:1) or use #767676 to match the text-safe palette.
Exercise
A primary button has white 16px regular text on a fill with a 3.4:1 ratio. The designer argues the button is "large" because it is wide. Does the 3:1 large-text threshold apply?
Show answer
No. Large text is defined by font size and weight (at least 24 CSS pixels regular, or 18.66 CSS pixels bold), not by the size of the element. At 16px regular the label is normal text and needs 4.5:1, so the pair fails. Darken the fill or enlarge and bolden the label to meet the large-text definition.