Midday, you open a ride-hailing app at the roadside and the sun hits your screen. A pale green button with white text almost disappears, while black text on a white background still reads clearly.
What decides whether text can be read is contrast: the difference in lightness between text and its background. That difference is what makes text readable, not its colour. Picture the board in a classroom. White chalk on a black board reads from the back row. Yellow marker on a whiteboard, bright as it is, makes you walk up to it. Yellow and white are both light, so the difference between them is small. On the web, this difference is measured with one number: the contrast ratio.
The contrast ratio and its thresholds
The contrast ratio runs from 1:1 (exactly the same colour) to 21:1 (black on white). WCAG 2.2, the web accessibility standard from the W3C (the organisation that writes web standards), sets the thresholds. The criterion column holds the number of each rule in WCAG:
| What is measured | Criterion | Level AA | Level AAA |
|---|---|---|---|
| Normal text | 1.4.3 and 1.4.6 | 4.5:1 | 7:1 |
| Large text: at least 24px, or at least 18.66px bold | 1.4.3 and 1.4.6 | 3:1 | 4.5:1 |
| UI components and graphics: button borders, focus rings, icons | 1.4.11 | 3:1 |
- WCAG writes large text sizes in points: 18pt, or 14pt bold. In CSS, 1pt = 1.333px, so that gives 24px and 18.66px.
- AA is the level usually asked for. AAA is stricter.
- What's exempt from these thresholds: inactive components (a button that can't be pressed), pure decoration, and text inside a logo.
Computing the contrast ratio
Mrs. Sri once wanted her book button in a brighter green, #22c55e, with white text. The green glows and the text is clean white. Does it pass the WCAG standard? Guess first, then see how it's computed.
The ratio is computed from relative luminance (L): how light a colour is, from 0 (black) to 1 (white). The hex code #15803d, the green of Mrs. Sri's current button, is a way to write a colour with # and six digits and letters. It holds three numbers from 0 to 255, one for each colour channel: red (R) 21, green (G) 128 and blue (B) 61. These are the steps to compute L for #15803d:
- Divide each channel by 255: R 21 becomes 0.0824, G 128 becomes 0.5020, B 61 becomes 0.2392.
- Convert each result of step 1 (call it c) into an amount of light: if c ≤ 0.04045 then c ÷ 12.92, otherwise ((c + 0.055) ÷ 1.055) to the power 2.4. That gives R 0.00750, G 0.21586, B 0.04667.
- L = 0.2126 × R + 0.7152 × G + 0.0722 × B = 0.00159 + 0.15438 + 0.00337 = 0.1593.
- Ratio = (lighter L + 0.05) ÷ (darker L + 0.05). The lighter L is the L of the lighter colour. White has L = 1, so (1 + 0.05) ÷ (0.1593 + 0.05) = 5.02:1.
Green gets the weight 0.7152 because the eye is most sensitive to green. Here is the same formula in JavaScript, which the Lab uses too:
function luminance(hex) {
const [r, g, b] = [1, 3, 5].map((i) => {
const c = parseInt(hex.slice(i, i + 2), 16) / 255;
return c <= 0.04045 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4;
});
return 0.2126 * r + 0.7152 * g + 0.0722 * b;
}
function contrast(a, b) {
const [hi, lo] = [luminance(a), luminance(b)].sort((x, y) => y - x);
return (hi + 0.05) / (lo + 0.05);
}
console.log(contrast("#ffffff", "#15803d").toFixed(2)); // "5.02"
console.log(contrast("#ffffff", "#22c55e").toFixed(2)); // "2.28"
luminanceruns steps 1 to 3 for one hex colour.contrastsorts the two luminances so the lighter one comes first, then runs step 4.toFixed(2)writes the result with two digits after the decimal point, so it prints"5.02"and"2.28".- WCAG 2.0 and 2.1 wrote 0.03928 instead of 0.04045. For 8-bit colours (each channel from 0 to 255) the result is exactly the same.
So Mrs. Sri's bright green doesn't pass. #22c55e has L = 0.4108, so its ratio is (1.05) ÷ (0.4608) = 2.28:1, far below 4.5:1. That bright green is light, and white text is light too. The difference is small, like yellow marker on a whiteboard. And don't round up: 4.49:1 still fails.
Every pair on Mrs. Sri's page
Each text is measured against the background right behind it:
| Text or component | Colour on background | Size | Ratio | Threshold | Result |
|---|---|---|---|---|---|
| Heading "Mist Homestay" | #1f2937 on #fff |
22px bold (large) | 14.68:1 | 3:1 | passes |
| Room, check-in | #475569 on #fff |
16px | 7.58:1 | 4.5:1 | passes |
| Book button label | white on #15803d |
16px bold | 5.02:1 | 4.5:1 | passes |
| Note "Free cancellation..." | #475569 on #fff |
14px | 7.58:1 | 4.5:1 | passes |
| Text outside the card | #475569 on #f1f5f9 |
14px | 6.92:1 | 4.5:1 | passes |
| Night button border | #475569 on #fff |
component | 7.58:1 | 3:1 | passes |
| Book button background | #15803d on #fff |
component | 5.02:1 | 3:1 | passes |
- A 16px bold button label isn't large text yet (that needs 18.66px bold), so its threshold stays 4.5:1.
- If the note gets a pale fog-coloured grey,
#94a3b8, its ratio is only 2.56:1 and it fails.#64748b(4.76:1) or#475569(7.58:1) pass. - The card border
#e2e8f0is only 1.23:1 against white, and that's allowed. A card isn't a component you can press, so its border counts as decoration.
Don't rely on colour alone
Some people have trouble telling red from green. Large surveys reviewed by Birch (2012) found red-green colour blindness in about 8% of men and 0.4% of women of European descent. In Chinese and Japanese populations, the figure was 4% to 6.5% of men. WCAG criterion 1.4.1 (level A) asks that colour is not the only way information is shown.
An example on Mrs. Sri's page: available dates get a green dot #15803d, and fully booked dates, such as the 14th, get a red dot #b91c1c. Now simulate how a guest with deuteranopia sees them. Deuteranopia is red-green colour blindness where the eye has no green cones (the cells that sense green). The simulation uses the matrix (formula) of Machado, Oliveira and Fernandes (2009), computed in linear RGB, which means the amounts of light from step 2 above. The green becomes #756c42 and the red #776a12. Both are an olive brown, 1.03:1 apart. That guest can't tell that the 14th is full. The fix: write "full" and strike the date through, so the information reads without colour.
Lightness contrast stays safe: white text on the simulated green is still 5.28:1. That's why contrast is measured from lightness, not from hue (where a colour sits on the colour wheel). Chrome DevTools can simulate colour blindness in the Rendering panel, under "Emulate vision deficiencies".
The exercise: three levels of text
In the exercise, three texts on a white background are too pale: #D1D5DB on the heading and body text (1.47:1), and #E5E7EB on the caption (1.24:1). Swap them for these greys, which pass:
.heading { color: #111827; } /* 17.74:1 */
.body { color: #374151; } /* 10.31:1 */
.caption { color: #6B7280; } /* 4.83:1, still above 4.5:1 */
#111827is the darkest, for the heading (.heading).#374151is for the body text (.body), and#6B7280for the small caption (.caption).- In the exercise, the caption is written with
<small>, the tag for small print.<small>makes its text smaller than 16px, so it is clearly not large text. The threshold stays 4.5:1, and#6B7280passes with a little to spare.
📌 What about APCA?
APCA is a contrast method that was once proposed for WCAG 3. It accounts for font size and weight more precisely. But APCA was removed from the WCAG 3 draft in July 2023, and the latest draft says the WCAG 3 contrast algorithm is yet to be determined. WCAG 3 itself is still a draft, not a standard. Accessibility audits and legal requirements today use the WCAG 2.x ratio (the numbers are the same in 2.0, 2.1 and 2.2).
⚠️ Contrast that looks fine but fails
- A bright colour mistaken for high contrast.
#22c55eglows, but with white text it's only 2.28:1. Measure, don't guess. - A pale placeholder. A placeholder (the sample text inside an empty form field) is text people must read too, so it still needs 4.5:1.
- Rounding up. 4.49:1 fails, even though it looks like 4.5.
- Status shown only by colour. Colour-blind users can't tell green and red dots apart when there are no words. Add text or an icon.
- Text on a photo. On a white foggy photo, white text vanishes in places. Put a dark layer behind the text, then measure the contrast over the lightest part of the photo.
TL;DR: The contrast ratio is the difference in lightness between text and its background, as one number: (lighter L + 0.05) ÷ (darker L + 0.05). L is the relative luminance, how light a colour is, from 0 (black) to 1 (white), and the lighter L belongs to the lighter colour. WCAG 2.2 level AA, the level usually asked for: normal text 4.5:1, large text (24px, or 18.66px bold) 3:1, UI components 3:1. Level AAA: 7:1. On Mrs. Sri's page: white text on #15803d is 5.02:1 and passes, on #22c55e it is 2.28:1 and fails, the #475569 note is 7.58:1 and passes. Don't round up. Don't rely on colour alone (WCAG 1.4.1): with deuteranopia, one kind of red-green colour blindness, green and red both look olive brown. So add text or an icon for colour-blind users. APCA, a contrast method that was once proposed, was removed from the WCAG 3 draft in 2023, so use the WCAG 2.x ratio.
⚡ The WCAG thresholds
Meaning: The contrast ratio is the difference in lightness between text and its background, from 1:1 to 21:1. WCAG 2.2 (the web accessibility standard) level AA, the level usually asked for, needs 4.5:1 for normal text, 3:1 for large text (24px, or 18.66px bold), and 3:1 for UI components such as button borders and icons. Don't round up: 4.49:1 still fails.
⚡ Bright doesn't mean contrast
Guess first: a bright green #22c55e button with white text. Does it pass WCAG?
No, it's only 2.28:1, and the threshold is 4.5:1. The green is light and white text is light too, so the difference is small. Mrs. Sri's green #15803d reaches 5.02:1.
Meaning: What makes text readable is the difference in lightness, not the colour. Measure the contrast, don't guess it.
⚡ Three levels of text that pass
Write:
.heading { color: #111827; } /* 17.74:1 */
.body { color: #374151; } /* 10.31:1 */
.caption { color: #6B7280; } /* 4.83:1, still above 4.5:1 */
Meaning: The more important the text, the darker its colour: the heading .heading is the darkest, then the body text .body, then the small caption .caption. A small caption still needs 4.5:1, and #6B7280 passes with a little to spare.
⚡ Don't rely on colour alone
Meaning: Some people have trouble telling red from green. A full date marked only with a red dot can look the same as a free one to a colour-blind user. Add the word "full" or an icon (WCAG rule 1.4.1).