Sub topic 12
Check contrast, text size, and structure
This chapter assumes you have a high-fidelity mockup with a type scale, a color palette, and a wireframe-based structure. It also assumes you can read basic HTML and CSS. You will check three things before the design goes to feedback: color contrast, text size, and page structure. Each check is cheap to run on a mockup. Each is expensive to fix after the page is built.
Why check at the mockup stage
Accessibility problems are design decisions. A pale gray caption, a 12px legal line, or a hero with no real heading is chosen in the mockup. A developer can only reproduce it faithfully. If you find the problem now, you change a color value or a size in one place. If you find it after launch, you change it across templates and components.
The checks in this chapter follow the Web Content Accessibility Guidelines (WCAG) 2.2, level AA. This is the level most organizations and many legal standards target.
Contrast
Contrast ratio compares the brightness of two colors. It runs from 1:1 (identical colors) to 21:1 (black on white). Low contrast makes text hard to read for people with low vision, and for anyone using a phone in sunlight.
WCAG AA sets these minimums:
| Element | Minimum ratio |
|---|---|
| Body text (under 24px regular, or under 18.66px bold) | 4.5:1 |
| Large text (24px or larger, or 18.66px or larger and bold) | 3:1 |
| UI components such as button borders, input borders, and focus rings | 3:1 against adjacent colors |
| Meaningful icons and chart elements | 3:1 |
Logos and purely decorative elements are exempt. Disabled controls are also exempt, although a disabled state that nobody can read is still a poor design.
Test the pairs you actually use
Do not test colors one at a time. Test each foreground and background pair that appears on the page. Use the tokens from the earlier chapter as your list. For the palette in this tutorial, the pairs look like this:
| Pair | Where it appears | Result |
|---|---|---|
| `#1a1a1a` on `#ffffff` | Body text | About 17:1, passes |
| `#0b5fff` on `#ffffff` | Links and outline buttons | About 5.6:1, passes |
| `#ffffff` on `#0b5fff` | Text inside the primary button | About 5.6:1, passes |
| `#8a8a8a` on `#ffffff` | Captions, if you add a muted gray | About 3.4:1, fails for body-size text |
The last row is the typical failure. A muted gray looks refined in a mockup and fails for small text. Darken it to about #6b6b6b, which gives roughly 5.3:1.
You can compute the ratio yourself. This script follows the WCAG formula, so you can check a whole token list at once.
def channel(value):
c = value / 255
return c / 12.92 if c <= 0.03928 else ((c + 0.055) / 1.055) ** 2.4
def luminance(hex_color):
h = hex_color.lstrip("#")
r, g, b = (int(h[i:i + 2], 16) for i in (0, 2, 4))
return 0.2126 * channel(r) + 0.7152 * channel(g) + 0.0722 * channel(b)
def contrast(fg, bg):
l1, l2 = luminance(fg), luminance(bg)
lighter, darker = max(l1, l2), min(l1, l2)
return (lighter + 0.05) / (darker + 0.05)
pairs = [
("#1a1a1a", "#ffffff", 4.5),
("#0b5fff", "#ffffff", 4.5),
("#ffffff", "#0b5fff", 4.5),
("#8a8a8a", "#ffffff", 4.5),
("#6b6b6b", "#ffffff", 4.5),
]
for fg, bg, needed in pairs:
ratio = contrast(fg, bg)
status = "pass" if ratio >= needed else "FAIL"
print(f"{fg} on {bg}: {ratio:.2f}:1 (needs {needed}) {status}")Common contrast traps
- Text over photos. The ratio changes across the image. Test the lightest and darkest areas behind the text. A dark overlay, such as a 50 percent black layer, gives a predictable background.
- Hover and focus states. Check each state's colors, not only the default. A hover color that is lighter than the default often fails.
- Placeholder text in form fields. Placeholders still count as text and need 4.5:1.
- Text on gradients. Test the worst point of the gradient.
- Color as the only signal. A link that differs from body text only by color fails for users who cannot distinguish the colors. Keep the underline, or add another cue.
Text size
WCAG does not set a minimum font size. Real readability still depends on it. Set these working limits for the mockup:
- Body text at 16px (1rem) or larger. The CSS in this tutorial uses
--font-size-body: 1rem. - Secondary text such as captions at 14px (0.875rem) at the smallest.
- Legal and footer text at 14px where possible. If you must go smaller, never below 12px, and keep a strong contrast ratio.
- Line height of at least 1.5 for body paragraphs. WCAG text spacing guidance (1.4.12) expects content to survive a line height of 1.5 without clipping.
- Line length of roughly 45 to 75 characters for paragraphs.
Use relative units
Design tools show pixels. Production CSS should use rem so that text follows the user's browser setting. A user who sets a larger default size should see larger text. If the page fixes sizes in px, it can ignore that choice.
WCAG 1.4.4 requires that text can be resized to 200 percent without loss of content or function. Test this by zooming your mockup, or the built page, to 200 percent. Check for three problems:
- Text that overflows its container.
- Buttons whose labels wrap and get clipped.
- Fixed-height boxes that cut off the second line.
The fix is usually to let containers grow with their content. Prefer min-height over height, and use padding from your spacing scale instead of a fixed box size.
.button {
display: inline-block;
min-height: 2.75rem;
padding: var(--space-1) var(--space-3);
font-size: var(--font-size-body);
line-height: 1.5;
}
.caption {
font-size: 0.875rem;
line-height: 1.5;
color: #6b6b6b;
}The button also meets target size. WCAG 2.2 criterion 2.5.8 asks for interactive targets of at least 24 by 24 CSS pixels. A height of 2.75rem is 44px at default settings, which matches the common touch guideline of 44px.
Structure
Structure is what assistive technology reads. A screen reader user does not see your layout. They move through headings, landmarks, links, and form fields. A mockup that looks organized can still produce a confusing page if the underlying order is wrong.
Headings
Check these rules against your mockup's text styles:
- One
h1per page. It carries the hero promise. - Headings follow a logical order. An
h2introduces each major section, andh3sits inside it. Do not jump fromh1toh4because you like the smaller size. - Style and level are separate choices. If a design calls for a small heading in a high-level position, give it the correct level and set its size with CSS.
A useful test: collect only the headings of your page into a list. If the outline tells the story of the page, the structure works.
Landmarks and reading order
The wireframe skeleton from earlier already uses landmark elements: header, nav, main, and footer. Landmarks let screen reader users jump straight to a region. If a page has more than one nav, give each a label, as the skeleton does with aria-label="Main".
Reading order follows the order in the HTML, not the visual position. If a design places an image on the left and the text on the right on desktop, the source order should still make sense when read from top to bottom. Avoid CSS tricks that reorder content in ways that disagree with the source order, because keyboard focus then jumps around the screen.
Keyboard and focus
Mark up your mockup for keyboard users:
- Every link, button, and field must have a visible focus state. Design it. Do not leave it to the browser default by accident.
- The focus indicator needs 3:1 contrast against its surroundings (WCAG 2.4.11 and 1.4.11).
- The tab order should match the visual order: logo, navigation, hero button, then the rest.
- A "Skip to main content" link at the top lets keyboard users bypass the navigation.
a:focus-visible,
button:focus-visible {
outline: 3px solid var(--color-accent);
outline-offset: 2px;
}
.skip-link {
position: absolute;
left: var(--space-2);
top: -3rem;
padding: var(--space-1) var(--space-2);
background: var(--color-bg);
}
.skip-link:focus {
top: var(--space-1);
}The skip link is visible only when it receives focus. Put it as the first element inside body, pointing at #main. Add id="main" to the main element.
Links, buttons, and images
- Link text should make sense out of context. "Read the pricing guide" works. "Click here" does not.
- Buttons perform actions, and links navigate. Mark the hero's "Get started" as a link, since it goes to
/signup. - Every informative image needs a text alternative. Note alt text in the mockup for each image. Decorative images get empty alt text so screen readers skip them.
A review workflow
Run the checks in this order. Each step catches problems that would otherwise hide behind the next one.
Fix failures at the token level when you can. Darkening --color-muted once repairs every caption. This is why the earlier chapter stored decisions as tokens.
Common mistakes
- Testing only the default state and skipping hover, focus, and disabled-looking states.
- Treating an automated score as proof. Automated tools find only a portion of accessibility problems. Contrast and missing alt text are easy to detect. Reading order and meaningful link text need a human.
- Choosing heading levels by size instead of by outline.
- Leaving accessibility notes in a private file. Put contrast results, alt text, and focus designs in the mockup so the handoff carries them.
Exercise
Your mockup places 14px text in #8a8a8a on a white background. It measures about 3.4:1. State whether it passes WCAG AA, and give two ways to fix it.
Show answer
It fails. Body-size text needs 4.5:1, and 14px regular text is not large text, so the 3:1 allowance does not apply. Fix it by darkening the gray, for example to #6b6b6b (about 5.3:1), or by changing the background. Raising the size to 24px would also qualify as large text, but that is rarely right for a caption, so darkening the color is the usual fix.
Exercise
A designer wants a small, quiet section label, so the page goes from an h1 straight to an h4 styled at 14px. What is wrong, and what should the markup be?
Show answer
Skipping from h1 to h4 breaks the heading outline, so screen reader users get a misleading structure. Use an h2 (or an h3 if it sits inside an h2 section) and make it look small with CSS, for example font-size: 0.875rem. Level carries meaning, and size carries style.
Exercise
You zoom the built page to 200 percent and the "Get started" button label is cut off. The button has height: 2.75rem. What change fixes it, and which WCAG criterion does the failure break?
Show answer
Replace height with min-height: 2.75rem and keep padding from the spacing scale, so the button grows when the label wraps. The failure breaks 1.4.4 Resize Text, which requires text to scale to 200 percent without loss of content or function.