Sub topic 8
Contrast and Resizing Text
This chapter assumes you know basic HTML and CSS, including font-size, color, and rem units. It also assumes you have read the earlier chapters on the type scale, body text, captions, and labels. Those chapters gave each text role its own size and weight. This one checks that readers can still see and scale that text. A hierarchy that looks right to you can fail for someone with low vision, a glare on their screen, or a larger default font size.
Why contrast and resizing belong in a hierarchy
Size and weight only separate headings from body text if the reader can see the text. Captions and labels are the usual casualties. Designers often make secondary text pale gray to push it back visually. Pushing it back too far makes it unreadable.
Two WCAG 2 success criteria cover this chapter:
- 1.4.3 Contrast (Minimum) sets the minimum contrast between text and its background.
- 1.4.4 Resize Text requires text to scale up to 200% without losing content or function.
WCAG is the Web Content Accessibility Guidelines. Level AA is the usual target for legal and organizational requirements.
How contrast is measured
A contrast ratio compares the relative luminance (perceived brightness) of two colors. It ranges from 1:1 (identical colors) to 21:1 (black on white).
The formula is (L1 + 0.05) / (L2 + 0.05). L1 is the lighter color's luminance and L2 is the darker one's. Luminance comes from the red, green, and blue channels after converting them from sRGB to linear light.
The thresholds at level AA:
| Content | Minimum ratio |
|---|---|
| Normal body text | 4.5:1 |
| Large text (at least 24px, or 18.66px and bold) | 3:1 |
| UI component borders and focus indicators | 3:1 |
| Logos, and disabled controls | No requirement |
Level AAA (criterion 1.4.6) raises these to 7:1 for normal text and 4.5:1 for large text. Aim for AAA on long reading text when your design allows it.
Large text gets a lower threshold because bigger glyphs stay legible at lower contrast. This helps headings but not captions. Captions are small, so they must meet 4.5:1.
Computing a ratio yourself
Browser developer tools and design tools report contrast for you. Writing the calculation once shows what they do. This script runs on Python 3 with no dependencies.
def channel(value):
c = value / 255
return c / 12.92 if c <= 0.04045 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 = sorted((luminance(fg), luminance(bg)), reverse=True)
return (l1 + 0.05) / (l2 + 0.05)
for color in ("#595959", "#767676", "#999999"):
print(color, "on #ffffff:", format(contrast(color, "#ffffff"), ".2f"))The three grays give these approximate ratios on white:
| Text color | Ratio on white | Normal text (4.5:1) | Large text (3:1) |
|---|---|---|---|
| `#595959` | 7.00:1 | Pass | Pass |
| `#767676` | 4.54:1 | Pass, barely | Pass |
| `#999999` | 2.85:1 | Fail | Fail |
#767676 is the lightest pure gray that passes 4.5:1 on white. That is why it appears so often in style guides. #999999 is a common choice for captions and placeholders, and it fails.
Applying contrast to each text role
Check every role separately. Each one tends to fail in its own way.
Headings
Headings are large or bold, so the 3:1 threshold may apply. Do not rely on that. A heading that is only just legible weakens the page's main signal. Give headings the same dark color as body text, or darker.
Body text
Body text carries most of the content and needs at least 4.5:1. Pure black on pure white (21:1) can feel harsh for long reading. Many designs use a dark gray such as #1f1f1f on white, which stays far above the minimum.
Captions and secondary text
Captions sit at the bottom of the hierarchy, so they are the most tempting place to cut contrast. The figcaption example in this tutorial uses #595959 for that reason. It still reads as quieter than the body text but gives 7:1 on white. Reduce emphasis with a smaller size and a lighter weight before you reduce contrast.
Labels and form text
Labels follow the same 4.5:1 rule. Two related cases are often missed:
- Placeholder text is the hint inside an empty field. Browsers default it to a light gray that often fails. Style it to at least 4.5:1, and never use it in place of a label.
- Input borders need 3:1 against the background, so users can find the field. A 1px
#dddborder on white does not meet that.
:root {
--text-color: #1f1f1f;
--text-muted: #595959;
--border-control: #767676;
--bg: #ffffff;
}
body {
color: var(--text-color);
background: var(--bg);
}
figcaption {
color: var(--text-muted);
}
input,
select,
textarea {
border: 1px solid var(--border-control);
}
::placeholder {
color: var(--text-muted);
opacity: 1;
}The opacity: 1 line matters. Some browsers apply a default opacity to placeholders, which lowers the contrast of whatever color you set.
Text over images and gradients
Contrast is measured against the actual pixels behind the text. If a heading sits on a photo, check the lightest and darkest areas behind it. The usual fixes are a semi-transparent overlay behind the text or a solid panel. A text shadow does not reliably count as a fix.
Do not rely on color alone
Color contrast is not the same as color meaning. If an error label is red and nothing else marks it, a reader who cannot see the color difference gets no signal. Add an icon, a prefix like "Error:", or a different weight. WCAG covers this under criterion 1.4.1 (Use of Color).
Resizing text
People enlarge text in three ways, and your CSS has to survive all of them:
- Browser zoom scales everything (Ctrl/Cmd and
+). Layout and text grow together. - Default font size is a browser setting that changes what
1remequals. It affects only text sized in relative units. - Text-only zoom is offered by some browsers and operating systems and enlarges text without scaling the layout.
Sizing with px for text breaks the second and third methods. In most browsers a font-size: 16px declaration ignores the user's default setting. That is why this tutorial's type scale uses rem.
The root font size rule
Do not set a pixel size on html. If you must set one, use a percentage.
/* Avoid: ignores the user's default font size */
html { font-size: 16px; }
/* Safe: keeps the user's setting as the base */
html { font-size: 100%; }With the 100% form, 1rem equals whatever size the reader chose, and your whole scale follows.
What breaks at 200%
A layout can have correct units and still fail when text doubles. Watch for these:
- Fixed heights on buttons, cards, or headers. Text overflows or is cut off. Use
min-heightor let the content set the height. overflow: hiddenon a container with a fixed size. It hides the overflowing text.- Fixed-width columns that cause horizontal scrolling. Criterion 1.4.10 (Reflow) expects content to fit a 320 CSS pixel wide viewport, which is roughly a 1280px window at 400% zoom, without scrolling in two directions.
- Absolutely positioned text that overlaps other elements once it grows.
- Line height in pixels. A
line-height: 20pxon text that grows to 40px makes lines collide. Use a unitless value such as1.5.
/* Fragile: fixed height and pixel line height */
.button-bad {
height: 40px;
line-height: 40px;
font-size: 14px;
overflow: hidden;
}
/* Resilient: size follows the text */
.button {
min-height: 2.5rem;
padding: 0.5rem 1rem;
line-height: 1.25;
font-size: 0.875rem;
}Text spacing overrides
Criterion 1.4.12 (Text Spacing) says users may override spacing with their own stylesheet, and the page must keep working. The values it tests are:
- Line height at least 1.5 times the font size.
- Spacing after paragraphs at least 2 times the font size.
- Letter spacing at least 0.12 times the font size.
- Word spacing at least 0.16 times the font size.
You do not need to set these values yourself. You need to avoid layouts that break when someone else does. Fixed-height boxes around text are again the main cause.
Responsive sizing and zoom
The clamp() function shown earlier for headings uses a vw unit in the middle term. A purely viewport-based size, such as font-size: 3vw, does not respond to browser zoom the way rem does, which can block 200% resizing. The earlier example avoids this because the rem terms are in the formula. Keep a rem term in any fluid size.
h1 {
font-size: clamp(1.75rem, 1.2rem + 2vw, 3rem);
}The 1.2rem part scales with the user's setting, and the minimum and maximum are both rem too.
Contrast settings beyond your control
Some readers use operating-system settings that change colors. CSS has hooks for them:
@media (prefers-contrast: more) {
:root {
--text-muted: #3d3d3d;
--border-control: #404040;
}
}
@media (forced-colors: active) {
figcaption {
color: CanvasText;
}
}prefers-contrast: more lets you strengthen your tokens for readers who ask for it. forced-colors: active applies in modes such as Windows High Contrast, where the browser replaces your colors. Mostly you should stay out of the way in that mode. Avoid relying on background images or box-shadow alone to carry meaning, because forced colors may remove them.
A testing workflow
The diagram shows the order of checks. It starts with automated contrast tests and ends with manual zoom tests, because the automated tests cannot find layout failures.
In practice:
- Use your browser's developer tools. Inspecting an element and clicking its color swatch shows the ratio and a pass/fail mark.
- Run an automated checker such as Lighthouse or axe. These catch ratios on static text but cannot judge text over images or states like hover and focus.
- Zoom the page to 200%, then to 400%, and look for clipped, overlapping, or horizontally scrolling content.
- Raise the browser's default font size to "Very large" and reload. Check that
rem-based text grew. - Check interactive states, such as hover, focus, and error. Each state needs its own contrast.
Common mistakes
- Choosing caption or placeholder gray by eye on a bright monitor. Measure it.
- Treating 3:1 for large text as a license for light headings.
- Setting
html { font-size: 62.5%; }as a shortcut for easy math. It works withrem, but any component sized inpxstays at one size and the scale becomes inconsistent. If you use it, size all text inrem. - Using
opacityto make text look muted. The text color blends with whatever is behind it, so the real contrast is hard to know. Use a solid color instead. - Testing zoom only on the home page. Forms and data tables break first.
Exercise
A caption uses color: #999999 on a white background at font-size: 0.875rem. Does it pass WCAG AA, and what is one change that fixes it without making the caption as prominent as the body text?
Show answer
It fails. #999999 on white is about 2.85:1, and the caption is small text that needs 4.5:1. Changing the color to #595959 (about 7:1) fixes it. The caption stays quieter than body text because it keeps its smaller size and lighter color, just a darker one than before.
Exercise
A card has height: 120px; overflow: hidden; and holds a heading and a paragraph sized in rem. At 200% zoom the paragraph's last lines disappear. Explain why, and rewrite the height rule so the card works.
Show answer
The text grows with zoom, but the fixed height does not leave room for it, and overflow: hidden hides whatever no longer fits. Replace the fixed height with min-height: 7.5rem; (120px at the default size) and remove overflow: hidden, so the card grows with its content.