Sub topic 2
Measuring Contrast with WCAG Ratios
Assume you know basic HTML and CSS, including selectors, custom properties, and the box model. This chapter focuses on how contrast and placement guide attention.
Measuring contrast with WCAG ratios
Contrast is the difference in brightness between foreground and background colors. The Web Content Accessibility Guidelines (WCAG) express it as a ratio from 1:1 to 21:1. Higher contrast makes text easier to read.
For common WCAG Level AA checks:
| Content | Minimum ratio |
|---|---|
| Normal text | 4.5:1 |
| Large text | 3:1 |
| User-interface components | 3:1 |
Large text usually means at least 24 pixels, or 18.66 pixels when bold. Check the exact WCAG definition when compliance matters.
Use a contrast checker during design and testing. The calculation compares the relative luminance of each color:
function channel(value) {
const s = value / 255;
return s <= 0.04045
? s / 12.92
: ((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(colorA, colorB) {
const a = luminance(colorA);
const b = luminance(colorB);
return (Math.max(a, b) + 0.05) / (Math.min(a, b) + 0.05);
}
console.log(contrastRatio([0, 0, 0], [255, 255, 255])); // 21Contrast also creates visual hierarchy. A dark primary button on a light surface attracts more attention than a pale secondary link. Do not use color alone to communicate meaning. Add text, icons, borders, or position.
How people scan a page
People often scan instead of reading every word. They look for headings, short paragraphs, familiar controls, and strong visual cues. A clear heading structure and visible primary action help users build a mental map quickly.
A common scan pattern is:
Position: where an element sits shapes its priority
Placement affects importance. Content near the top of the page, near the start of a reading line, or inside a visually isolated group usually receives attention first. Place the page's main purpose and primary action where users expect to find them.
Keep visual order consistent with document object model order. The document object model, or DOM, is the HTML structure that browsers and assistive technologies read. Avoid moving a later button before earlier content with CSS alone, because keyboard and screen reader users may encounter a different sequence.
Exercise
A paragraph uses dark gray text on a white background and measures 5.2:1. Does it meet the common WCAG AA minimum for normal text?
Show answer
Yes. Its 5.2:1 ratio is greater than the 4.5:1 minimum.