Sub topic 12
Keeping fluid layouts accessible
This chapter assumes you can write clamp(), min(), and max() expressions, and that you know rem, vw, and %. Fluid layouts adapt to the screen, but they can also work against people who zoom the page or change their default font size. Here you will learn which values break those settings and how to test for them.
What users change, and what the standards ask
Users adjust a page in three ways:
- Browser zoom (Ctrl and +) scales everything. The viewport also gets narrower in CSS pixels.
- Default font size is a browser setting. It changes the root font size, so every
remvalue changes with it. - Text-only zoom or user style sheets change spacing and font sizes without changing the layout width.
Three WCAG 2.x success criteria apply to fluid layouts:
| Criterion | Requirement | What it means for fluid CSS |
|---|---|---|
| 1.4.4 Resize Text | Text can be resized to 200% without loss of content or function | Font sizes must respond to zoom and settings |
| 1.4.10 Reflow | At 320 CSS px width (about 400% zoom on a 1280px window), content needs no horizontal scrolling | Containers and grids must shrink below their preferred size |
| 1.4.12 Text Spacing | Content survives larger line height, paragraph spacing, letter spacing, and word spacing | Avoid fixed heights on text containers |
Why viewport-only type is a problem
Zooming to 200% halves the viewport width in CSS pixels. A vw length is a fraction of that width, so it shrinks by the same factor that zoom enlarges it. The text barely grows on screen.
Compare three font sizes in a 1280px-wide window. Sizes are in device pixels, so they include the zoom factor.
| Declaration | At 100% zoom | At 200% zoom | Growth |
|---|---|---|---|
| `clamp(1rem, 4vw, 2rem)` | 32 | 51.2 | 1.6× |
| `clamp(1rem, 0.5rem + 3vw, 2rem)` | 32 | 54.4 | 1.7× |
| `2rem` | 32 | 64 | 2× |
At 100%, the first value is 4vw = 51.2px, which the maximum caps at 32px. At 200%, the viewport is 640 CSS px wide, so 4vw = 25.6 CSS px. That is 51.2 device px, only 1.6 times larger.
The rem term in the second row helps a little. It also makes the size respond to the default font size setting, which a vw-only preferred value ignores completely.
/* Harder to zoom: the preferred value ignores font-size settings */
.title-viewport-only {
font-size: clamp(1rem, 4vw, 2rem);
}
/* Better: the rem term responds to zoom and user settings */
.title {
font-size: clamp(1rem, 0.5rem + 3vw, 2rem);
}Rules for accessible fluid type
- Always include a
remterm in the preferred value. Write0.5rem + 3vw, not3vw. - Use
remfor the minimum and maximum, notpx. Pixel bounds do not follow the user's default font size. - Keep the
vwcoefficient small. The smaller the slope, the closer the text grows to a full 2× on zoom. - Keep the maximum a modest multiple of the minimum. A commonly cited rule of thumb is about 2.5 times or less. A large range means the size depends on the viewport more than on the user's settings.
- Do not use
vwalone for body text. Reserve larger ranges for headings. - Leave the root font size alone.
html { font-size: 62.5%; }is acceptable, because it is a percentage and still follows the user's setting.html { font-size: 10px; }is not.
html {
/* Percentage of the user's default, not a fixed pixel size */
font-size: 100%;
}
body {
/* Body text: tiny range, mostly rem */
font-size: clamp(1rem, 0.95rem + 0.25vw, 1.125rem);
line-height: 1.5;
}
h1 {
/* Headings can vary more, but still have a rem term */
font-size: clamp(1.75rem, 1.25rem + 2vw, 3rem);
}Reflow: nothing may force horizontal scrolling
Criterion 1.4.10 is where min() earns its place. At 400% zoom, a 1280px window becomes 320 CSS px wide. Any fixed width above that causes sideways scrolling.
.container {
/* 1rem gutter on each side, never wider than 70rem */
width: min(100% - 2rem, 70rem);
margin-inline: auto;
}
.cards {
display: grid;
gap: 1rem;
/* min(100%, 16rem) lets one column shrink below 16rem */
grid-template-columns: repeat(auto-fit, minmax(min(100%, 16rem), 1fr));
}
img,
video {
max-width: 100%;
height: auto;
}The min(100%, 16rem) guard in the grid matters most here. Without it, minmax(16rem, 1fr) has a 16rem floor. The floor can be wider than the container at 320px, so the grid overflows.
Other causes of overflow to check:
- A fixed
widthinpxon an element, such aswidth: 400px. Usemax-widthinstead. - Long unbroken strings, such as URLs. Add
overflow-wrap: anywhereoroverflow-wrap: break-wordto prose containers. - Flex items that refuse to shrink. Add
min-width: 0to a flex or grid child that contains wide content. - Wide tables and code blocks. These are allowed to scroll inside their own container, so wrap them in an element with
overflow-x: auto.
Text spacing and fixed sizes
Criterion 1.4.12 lets users raise line height to 1.5, paragraph spacing to 2× the font size, letter spacing to 0.12em, and word spacing to 0.16em. If a box has a fixed height, the larger text spills out or gets clipped.
.card {
/* Bad: height: 8rem; */
min-height: 8rem;
padding: clamp(1rem, 0.75rem + 1.25vw, 2rem);
}
article p {
/* ch is relative to the font, so line length follows the text size */
max-width: 65ch;
}Use min-height rather than height, and ch or rem rather than px for measures. Both grow with the text.
Media queries should use `em`
If you still use breakpoints alongside fluid values, write them in em. In browsers, em in a media query refers to the user's default font size, not a fixed pixel count. A user with a larger default font then gets the narrow layout sooner, which is what they need.
/* 48em is 768px at the default 16px, and larger for users with bigger text */
@media (min-width: 48em) {
.layout {
display: grid;
grid-template-columns: 16rem 1fr;
}
}Testing checklist
- Open the page at 1280px wide, then zoom to 200%. Text should be clearly larger and nothing should overlap.
- Zoom to 400%, or resize the window to 320px wide. Scroll vertically only. Check that images, cards, and code do not push the page wider.
- Change the browser's default font size to Large or Very Large (not zoom). Body text and headings should both grow. A heading that does not grow probably has a
vw-only preferred value or apxbound. - Apply a text-spacing bookmarklet or user style sheet and check for clipped text.
- In DevTools, check for elements wider than the viewport. Look at
document.documentElement.scrollWidthcompared withclientWidth.
// Run in the console at 320px wide: lists elements causing horizontal overflow
const overflowing = [...document.querySelectorAll("body *")].filter(
(el) => el.getBoundingClientRect().right > document.documentElement.clientWidth
);
console.log(overflowing);Exercise
A page uses h1 { font-size: clamp(16px, 5vw, 40px); }. List three changes that make it follow the user's browser settings better, and say what each one fixes.
Show answer
(1) Change the bounds to rem, for example clamp(1rem, ..., 2.5rem), so the minimum and maximum follow the user's default font size. (2) Add a rem term to the preferred value, for example 0.5rem + 3vw, so the middle of the range also responds to font-size settings and to zoom, instead of depending on the viewport width alone. (3) Reduce the vw coefficient, for example from 5vw to 3vw, so the size depends less on the viewport and grows closer to 2× when the user zooms to 200%.
Exercise
A grid uses grid-template-columns: repeat(auto-fit, minmax(18rem, 1fr)); and scrolls sideways at 320px width. What is the cause, and how do you fix it?
Show answer
At 320 CSS px the container is narrower than the 18rem (288px at the default size) column minimum once padding and gutters are subtracted, or the user's larger default font makes 18rem wider still, so the single column overflows. Wrap the minimum in min(): minmax(min(100%, 18rem), 1fr). The column can then shrink to the container's width.
Exercise
Why is .card { height: 8rem; } a problem for the Text Spacing criterion, and what do you use instead?
Show answer
Increasing line height, paragraph spacing, or letter spacing makes the text taller, and a fixed height clips it or lets it overflow into neighbouring content. Use min-height: 8rem so the box can grow with its content.