Sub topic 8
Color values and contrast
This chapter assumes you can open DevTools and read a color from a design file. It also assumes you know the hex, rgb() and hsl() notations. The goal is to confirm that the built page uses the designed colors and that text stays readable.
Compare values, not impressions
Your eye cannot tell #2563eb from #2461ea. Read the value from the page and compare it with the design token or spec.
DevTools always reports computed colors as rgb() or rgba(), even if the stylesheet used hex. Convert one notation to the other before you compare. The snippet below reads the computed colors of an element in the console.
const el = document.querySelector('.button-primary');
const s = getComputedStyle(el);
console.log({
color: s.color,
backgroundColor: s.backgroundColor,
borderColor: s.borderTopColor
});Common causes of a mismatch
- A hard-coded hex value was used instead of the design token, so one shade differs slightly.
opacitywas applied to the element. This fades its children too, so the text color looks lighter than the value in the CSS.- The background is
transparent, so the visible color comes from an ancestor. Walk up the DOM tree to find it. - The design tool and the browser handle color profiles differently, which can shift colors a little. Treat a difference of one or two units per channel as noise. Treat a visibly different hue as a bug.
- A gradient or an image sits behind the text. A single computed value cannot describe it.
Check contrast
The design may look right and still fail readers. WCAG (the Web Content Accessibility Guidelines) defines a contrast ratio from 1:1 to 21:1. The usual thresholds are:
| Element | Minimum ratio |
|---|---|
| Normal body text | 4.5:1 |
| Large text (24px, or about 18.7px bold) | 3:1 |
| Icons, input borders and other UI parts | 3:1 |
You can use the DevTools color picker, which shows the ratio. To see how the number is produced, run this function.
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. Gray #777777 on white looks fine but just misses the 4.5:1 requirement for body text.
A workflow for the pass
A contrast failure can come from the design itself, not from the build. In that case the page matches the design and still fails. Report it as a design problem. Do not call it a code bug. The triage chapter covers how to decide who owns it.
Exercise
A paragraph's CSS sets color: #333 but the page looks lighter than the design. The computed color in DevTools is rgba(51, 51, 51, 0.6). What is the likely cause, and where do you look?
Show answer
Something applied transparency, probably opacity on the paragraph or an ancestor, or an rgba() color. Check the Styles pane for opacity on the element and its parents. Then compare the result with the design token.