Sub topic 12
Keeping fluid layouts accessible
This chapter assumes you can write clamp(), min(), and max() expressions and know the difference between rem and vw. Fluid sizing can fail two groups of users: people who zoom the page and people who set a larger default font size in their browser. Both failures come from how the preferred value of a clamp() is built.
Why viewport-only type fails zoom
Browser zoom works by shrinking the viewport measured in CSS pixels. At 200% zoom, a 1200px-wide window becomes a 600px viewport. A size of 4vw then drops from 48px to 24px. The browser draws everything twice as large, so the text ends up at the same physical size as before. The user zoomed in and got no larger text.
A rem term does not have this problem. 1rem stays 16 CSS pixels at any zoom level, so it is drawn twice as large when the page is zoomed to 200%. It also follows the font size the user chose in browser settings.
/* Harder to zoom: the size depends only on the viewport */
.title-viewport-only {
font-size: clamp(1rem, 4vw, 2rem);
}
/* Better: the rem term responds to zoom and user font-size settings */
.title {
font-size: clamp(1rem, 0.5rem + 3vw, 2rem);
}Rules for accessible fluid values
| Rule | Reason |
|---|---|
| Put a `rem` term in every fluid font size | The text responds to zoom and to the user's default font size |
| Write the minimum and maximum in `rem`, not `px` | Pixel limits ignore the user's font setting |
| Keep the maximum within about 2.5 times the minimum | Zoom can still enlarge text even when the clamp is capped. Meeting this ratio is a common rule of thumb for passing WCAG 1.4.4 |
| Size spacing and containers in `rem` | Padding and gutters grow along with the text |
| Keep a `min(100% - 2rem, ...)` container | Content still fits at 320 CSS pixels, as WCAG 1.4.10 (Reflow) requires |
The maximum matters because of the cap. If the viewport is wide, clamp(1rem, 0.5rem + 3vw, 2rem) returns 2rem. At 200% zoom the viewport is narrower, so the preferred value is smaller and the result may fall below the cap. Because the result is then sized in rem, the text still ends up larger than before. A cap that is far above the minimum can make the text jump in size, so keep the range modest.
:root {
--step-0: clamp(1rem, 0.95rem + 0.25vw, 1.25rem);
}
body {
font-size: var(--step-0);
}
.container {
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}The .container keeps a 1rem gutter on each side. If the user raises their default font size, that gutter grows with it, and the content narrows instead of overflowing.
Testing
- Zoom the page to 200% and confirm the text is visibly larger than at 100%.
- Zoom to 400% on a desktop window about 1280px wide. The viewport is then 320 CSS pixels, and you should see no horizontal scrolling.
- Change the browser's default font size to "Large" or "Very large" and check that headings, padding, and gutters all grow.
Exercise
A heading uses font-size: clamp(16px, 5vw, 40px);. Give two reasons it is a poor choice for accessibility and write a better version.
Show answer
The preferred value 5vw depends only on the viewport, so browser zoom does not make the text larger. The px limits also ignore the user's default font size, and the maximum is 2.5 times the minimum, which is already the upper end of the usual guidance. A better version is font-size: clamp(1rem, 0.75rem + 2vw, 2.25rem);. It has a rem term in the preferred value and rem limits.