Sub topic 8
Fluid units: percentages, rem, vw, and clamp()
This chapter assumes you know basic HTML, CSS selectors and the cascade, the box model, and the units px, em, and rem. Media queries change a layout at fixed breakpoints. Fluid units make sizes change smoothly between those breakpoints, so you need fewer queries.
Fixed and fluid sizes
A pixel value never changes. A fluid value is calculated from something that does change, such as the parent's width, the root font size, or the viewport width. Four tools cover most cases.
| Unit | Relative to | Typical use |
|---|---|---|
| `%` | The parent element (width) or the containing block | Column widths, max widths |
| `rem` | The root element's font size | Font sizes, spacing |
| `vw` | 1% of the viewport width | Large headings, full-bleed sections |
| `clamp()` | A minimum, a preferred value, and a maximum | Bounded fluid sizes |
Percentages
A percentage width is a share of the parent's content width. If the parent grows, the child grows with it.
.page {
max-width: 60rem;
margin-inline: auto;
}
.main {
width: 70%;
}
.aside {
width: 30%;
}Percentages in padding and margin are also based on the parent's width, even for top and bottom values. Percentage height only works when the parent has an explicit height, which is a common source of confusion.
rem for text and spacing
1rem equals the root font size, usually 16px. Sizes in rem follow the user's browser font setting, so a person who raises their default text size gets larger text and spacing. Sizes in px ignore that setting.
body {
font-size: 1rem;
line-height: 1.5;
}
.card {
padding: 1.5rem;
border-radius: 0.5rem;
}vw and the accessibility trap
1vw is 1% of the viewport width. A viewport 800px wide gives 1vw = 8px. Using vw alone for text is risky. The text shrinks to unreadable sizes on small screens and grows without limit on large ones. Pure vw text also does not respond to browser zoom, which hurts users who need larger text.
/* Avoid: size depends only on the viewport */
h1 {
font-size: 5vw;
}clamp()
clamp(min, preferred, max) returns the preferred value, but never less than min and never more than max. Mix rem and vw in the preferred value so zoom still has an effect.
h1 {
font-size: clamp(1.5rem, 4vw + 1rem, 3rem);
}
.container {
width: clamp(18rem, 90%, 70rem);
margin-inline: auto;
}For the heading, the size is 1.5rem on narrow screens, grows with the viewport in the middle range, and stops at 3rem. The container is 90% of its parent, but never narrower than 18rem or wider than 70rem. No media query is involved.
Common errors
- Setting
html { font-size: 62.5%; }and then forgetting thatremvalues are scaled. Prefer leaving the root size alone. - Mixing up
emandrem.emcompounds through nested elements, whileremalways reads the root. - Using
100vwfor full-width elements. It includes the scrollbar width and can cause a horizontal scrollbar. Preferwidth: 100%. - Writing
clamp()arguments in the wrong order. The minimum comes first and the maximum last. - Writing
4vw+1remwithout spaces. Spaces around+and-are required insidecalc()andclamp().
Exercise
Write a rule that sets .lead text to at least 1rem, at most 1.5rem, and growing with the viewport in between. Why should the preferred value include a rem term?
Show answer
.lead { font-size: clamp(1rem, 0.75rem + 1vw, 1.5rem); }. The rem term keeps the size tied to the user's font settings, so browser zoom and larger default text still increase the size. A vw-only value would ignore them.