Sub topic 8
Color Values and Contrast
This chapter assumes you can read a design file for hex and RGB values, open DevTools, and read basic CSS color syntax. You should also know what the other chapters cover. Reading the design as a reference and measuring with DevTools come earlier. Triage and reporting come later.
Color checks answer two separate questions. First, does the built page use the colors the design specifies? Second, is the text readable against its background? A page can pass the first and fail the second, because the design itself may contain a low-contrast pair. It can also pass the second and fail the first.
Check the values against the design
Start from the design tokens or the style guide. A token is a named value, such as color-text-muted: #595959. If the design uses tokens, compare names and values. If it only has swatches, sample the color from the design file itself.
Then read what the browser actually computed. In the DevTools Elements panel, select the element and open the Computed tab. Look at color, background-color, border-color, and any fill or stroke on SVG icons.
Computed colors usually appear as rgb() or rgba(), even if the stylesheet used hex or hsl(). Convert before you compare. #595959 and rgb(89, 89, 89) are the same color.
You can read several values at once in the console:
const el = document.querySelector('.card__title');
const s = getComputedStyle(el);
console.log({
color: s.color,
backgroundColor: s.backgroundColor,
borderColor: s.borderTopColor
});Why a computed background can look wrong
background-color is often rgba(0, 0, 0, 0), which means transparent. The visible background then comes from an ancestor, an image, or a gradient. Walk up the tree until you find the element that paints the color you see.
Opacity causes a similar problem. If an element has opacity: 0.6, its text color value is unchanged, but the rendered color is blended with whatever is behind it. The computed color will match the design while the screen does not. Check opacity on the element and on its ancestors.
Common sources of mismatch
- Near-miss values. A developer picks
#333when the token is#2f2f2f. The difference is small, so look at the numbers instead of the screen. - Alpha instead of solid color. A design shows solid light gray, and the build uses black at 10% opacity. They match on white and differ on any other background.
- Color profile differences. A design tool may use Display P3 while the browser renders sRGB. Sampled values can differ slightly even when the CSS is correct. Confirm the design file's color profile before reporting a small difference.
- Hard-coded values. A color typed directly into a component instead of using the token will not change when the theme changes.
Sampling colors from a screenshot
When you cannot read the CSS, for example with an image or a canvas, sample the pixel. Use the browser's color picker in the DevTools style pane, or an OS-level color picker. Sample from a screenshot at 100% zoom with no scaling. Anti-aliased edges blend colors, so sample the center of a solid area, not the edge of a letter.
Contrast
Contrast ratio compares the relative luminance of two colors. WCAG 2.x defines it as (L1 + 0.05) / (L2 + 0.05), where L1 is the lighter color. The ratio runs from 1:1 (identical) to 21:1 (black on white).
The thresholds in WCAG level AA are:
| Content | Minimum ratio |
|---|---|
| Normal body text | 4.5:1 |
| Large text (18pt, or 14pt bold, and above) | 3:1 |
| User interface components and graphical objects, such as input borders and icons that carry meaning | 3:1 |
Logos and purely decorative elements are exempt. Disabled controls are also exempt, but that is not a reason to make them unreadable.
You can compute the ratio yourself:
function luminance([r, g, b]) {
const lin = (c) => {
c /= 255;
return c <= 0.04045 ? c / 12.92 : Math.pow((c + 0.055) / 1.055, 2.4);
};
return 0.2126 * lin(r) + 0.7152 * lin(g) + 0.0722 * lin(b);
}
function contrastRatio(fg, bg) {
const [hi, lo] = [luminance(fg), luminance(bg)].sort((a, b) => b - a);
return (hi + 0.05) / (lo + 0.05);
}
console.log(contrastRatio([119, 119, 119], [255, 255, 255]).toFixed(2));This prints 4.48. Mid-gray #777777 on white falls just under the 4.5:1 requirement for body text. It would pass for large text.
In practice, you will usually let DevTools do this. When you pick the text color in the style pane, Chrome and Firefox show the contrast ratio and mark whether it meets AA or AAA. The Accessibility panel also reports contrast for the selected element.
Contrast problems that are easy to miss
Tools that check a single pair of colors miss several cases.
- Text over images or gradients. The background changes across the text. Check the worst spot, usually where the image is lightest behind light text. A semi-transparent overlay behind the text is the usual fix.
- Placeholder text. Placeholders are often styled in a light gray. They need 4.5:1 if they carry information the user needs.
- Hover and focus states. A link might pass in its default state and fail when hovered. Check each state, as the later chapter on states describes.
- Dark mode or themes. Each theme is a separate set of pairs. Switch the theme and repeat.
- Borders and icons. Form field borders, icon-only buttons, and chart lines need 3:1 against their surroundings. A pale gray input border on a white page often fails.
- Color as the only signal. If an error is shown only by turning text red, a user who cannot distinguish red from the surrounding text gets no signal. Look for an icon, text, or underline alongside the color. You can test this by applying a color vision deficiency emulation in the DevTools Rendering panel.
Working through a pass
- List the color tokens in the design and note their values.
- Pick one representative element for each: body text, muted text, link, button, border, error message.
- Read each computed value in DevTools and compare it with the token.
- Check contrast for each text and background pair, in every state and theme.
- Record any failure with the element, both values, the measured ratio, and the required ratio.
When a contrast failure appears in the design too, do not silently change the build. The designer needs to decide on a new value. Report it as a design issue so both sides can agree on the fix.
Exercise
Body text on your page has the computed color rgb(119, 119, 119) on a white background. The design token for body text is #767676. Is this a color mismatch, a contrast failure, or both? What would you do next?
Show answer
It is a small color mismatch: #767676 is rgb(118, 118, 118), and the build uses 119. The difference of 1 is not visible, so it is low severity. For contrast, #777777 on white gives about 4.48:1, below the 4.5:1 AA requirement for normal text. The token #767676 gives about 4.54:1 and passes. So the build has a contrast failure that the design does not have. Report it as a build issue and ask for the token value to be used exactly.
Exercise
A card title is white text over a photo. The contrast checker shows 7:1 for the sample you picked. Why might this still fail, and how would you check?
Show answer
The photo behind the text varies in brightness, so the sampled point may be darker than other points under the text. Check the lightest region behind the text, or the whole text area, and measure there. If the ratio drops below 4.5:1 (or 3:1 for large text), report it and suggest a dark overlay or a different text placement.