Sub topic 8
Contrast and resizing text
This chapter assumes you know basic HTML and CSS, including color, font-size, and rem units. It also assumes you have seen the type scale from earlier chapters. A hierarchy only works if people can read it. Two things decide that: how much the text color differs from its background, and whether the text still works when a reader makes it bigger.
Contrast ratio
Contrast ratio compares the brightness of the text color with the brightness of the background. It runs from 1:1 (identical colors) to 21:1 (black on white). The Web Content Accessibility Guidelines (WCAG) set minimums for level AA:
| Text | Minimum ratio |
|---|---|
| Normal body text, captions, labels | 4.5:1 |
| Large text (24px or larger, or 18.66px or larger and bold) | 3:1 |
Small secondary text is where most failures happen. Captions and hint text are often set in light gray so they look less important. Lightening the color is a valid way to show lower importance, but only down to the minimum ratio.
/* Fails: #999999 on white is about 2.8:1 */
.caption-bad {
font-size: 0.875rem;
color: #999999;
}
/* Passes: #595959 on white is about 7:1 */
.caption-good {
font-size: 0.875rem;
color: #595959;
}Because captions are small, they get the stricter 4.5:1 limit, not the 3:1 limit for large text.
Checking a ratio
You can check pairs of colors with the contrast checker in browser developer tools or with any WCAG contrast calculator. In most browsers, selecting an element in the inspector and clicking its color swatch shows the ratio and whether it passes. Check each text role separately: body, headings, captions, labels, and placeholder text. Also check text on tinted backgrounds, such as a caption on a gray card.
Color should not be the only way to show a role. If a label is distinguished only by being red, readers who cannot see red lose the information. Pair color with size, weight, or position.
Resizing text
Readers resize text in two ways. They can zoom the whole page (Ctrl and +, or pinch zoom). They can also raise the default font size in their browser settings. WCAG asks that text can be enlarged to 200% without losing content or function.
Zoom usually works on its own. Changing the default font size works only if your sizes are relative. A rem is a unit equal to the root font size, which is 16px by default and follows the user's setting. A px value stays fixed.
/* Respects the user's default size */
:root {
--text-base: 1rem;
--text-sm: 0.875rem;
}
body {
font-size: var(--text-base);
line-height: 1.5;
}
.note {
font-size: var(--text-sm);
}
/* Avoid: ignores the browser's font-size setting */
.note-fixed {
font-size: 14px;
}Never set html { font-size: 12px; } to make rem math easier. That overrides the user's preference. If you want round numbers, use a percentage such as font-size: 100%, which keeps the user's value.
Layout also has to survive larger text. Three habits help:
- Use
min-heightinstead of a fixedheighton boxes that contain text. - Let text wrap. Avoid
white-space: nowrapon labels and buttons. - Set
line-heightas a unitless number such as1.5, so it scales with each element's font size.
.card {
min-height: 6rem;
padding: 1rem;
}
button {
padding: 0.5rem 1rem;
line-height: 1.2;
}A fixed height: 6rem would clip text that wraps onto more lines. min-height lets the box grow.
How the checks fit together
Common errors
- Light gray captions and placeholder text that fall under 4.5:1.
- White text on a photo or gradient without checking the lightest part of the background.
- Pixel font sizes that ignore the browser's default size setting.
- Fixed-height containers that clip text at 200% zoom.
- Disabling zoom with
user-scalable=noor a restrictivemaximum-scalein the viewport meta tag. Do not do this.
Exercise
A caption uses font-size: 14px; color: #999999; on a white background. Name two problems with this and write a corrected CSS rule.
Show answer
The color has a contrast ratio of about 2.8:1, below the 4.5:1 minimum for small text, and the pixel size ignores the user's default font-size setting. A corrected rule is figcaption { font-size: 0.875rem; color: #595959; }, which gives about 7:1 contrast and scales with the browser setting.
Exercise
A text box has height: 5rem and its label wraps to three lines at 200% zoom. What happens to the text, and what is the fix?
Show answer
The text overflows or is clipped because the box height is fixed. Replace height: 5rem with min-height: 5rem so the box grows with its content.