Emmanuel Akinfulubi

HTML and CSSPractical III

Check color contrast ratios (WCAG) in CSS

43 Contrast, actually measured

hard

Choose a palette and prove it. Compute the WCAG contrast ratio for each text and background pair you use, and check each against the level it has to reach.

Stuck? Three hints, smallest first
  1. 1. Body text needs 4.5:1. Text at 24px, or 18.66px bold, needs 3:1. Non-text boundaries need 3:1.
  2. 2. The formula is (lighter + 0.05) / (darker + 0.05) on relative luminance, not on the hex values.
  3. 3. A colour that passes on one background can fail badly on another, so the pair is what you test, never the colour.

index.html

<div class="panel">
  <h2 class="head">Fare summary</h2>
  <p class="body">Body copy at 4.5:1 or better.</p>
  <p class="muted">Secondary copy, still 4.5:1 or better.</p>
  <p><a class="cta" href="#book">Reserve a seat</a></p>
</div>

style.css

:root {
  --paper: #ffffff;
  --ink: #17171a;
  --muted: #5c5c66;
  --brand: #3055d8;
  --on-brand: #ffffff;
  --hairline: #8e8e98;
}

.panel {
  max-width: 260px;
  padding: 14px;
  border: 1px solid var(--hairline);
  border-radius: 10px;
  background: var(--paper);
  font-size: 0.85rem;
}

.head { margin: 0 0 6px; font-size: 1.5rem; color: var(--ink); }

.body { margin: 0 0 6px; color: var(--ink); }

.muted { margin: 0 0 10px; color: var(--muted); }

.cta {
  display: inline-block;
  padding: 8px 14px;
  border-radius: 6px;
  background: var(--brand);
  color: var(--on-brand);
  text-decoration: none;
}
Rendered result. Shown, not machine-checked: there is no browser in the build.
8 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 43

// The WCAG relative-luminance and contrast formulae, in full.
const lum = (hex) => {
  const h = hex.replace('#', '');
  const [r, g, b] = [0, 2, 4]
    .map((i) => parseInt(h.slice(i, i + 2), 16) / 255)
    .map((c) => (c <= 0.03928 ? c / 12.92 : ((c + 0.055) / 1.055) ** 2.4));
  return 0.2126 * r + 0.7152 * g + 0.0722 * b;
};
const ratio = (a, b) => {
  const [hi, lo] = [lum(a), lum(b)].sort((x, y) => y - x);
  return Math.round(((hi + 0.05) / (lo + 0.05)) * 100) / 100;
};
const token = (name) =>
  (new RegExp('--' + name + ':\\s*(#[0-9a-f]{6})', 'i').exec(css) || [, ''])[1];

test('body text on paper is 17.89 to 1', ratio(token('ink'), token('paper')), 17.89);
test('secondary text is 6.61 to 1, so it clears AA', ratio(token('muted'), token('paper')), 6.61);
test('white on the brand blue is 6.15 to 1', ratio(token('on-brand'), token('brand')), 6.15);
test('every text pair clears the 4.5 body-text threshold', [['ink', 'paper'], ['muted', 'paper'], ['on-brand', 'brand']].every(([f, b]) => ratio(token(f), token(b)) >= 4.5), true);
test('the hairline border clears the 3 to 1 needed for a boundary', ratio(token('hairline'), token('paper')) >= 3, true);
test('but it would NOT be usable as body text', ratio(token('hairline'), token('paper')) >= 4.5, false);
test('and a light blue that looks fine fails badly on white', ratio('#8ba4ff', '#ffffff'), 2.37);
test('the palette is declared once as tokens', (css.match(/--[a-z-]+:\s*#/g) || []).length, 6);

Output


      

These checks measure the actual palette rather than asserting a claim: the ratio is computed from the hex values in the stylesheet with the WCAG formula, so changing a token changes the number. Three things fall out of it. Contrast is a property of a pair, so the same blue can pass on white and fail on grey. The thresholds differ by role — 4.5 for body text, 3 for large text and for non-text boundaries like the border here, which is why --hairline passes as a border and is refused as text. And the last-but-one check is the common failure: a light accent that reads perfectly well on a dark background lands at 2.37:1 on white.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.