Sub topic 3
Thinking in relative units
This chapter assumes you know the CSS box model, how the cascade resolves conflicting rules, and how to inspect elements in browser developer tools. Relative units let a layout adapt to the user's settings and screen without a rule for every case. That makes them the base for mobile-first styles.
Absolute and relative lengths
A pixel (px) is a fixed length in CSS. A relative unit is calculated from something else: a font size, a container, or the viewport. Mobile-first CSS prefers relative units because the screen size and the user's text settings are unknown when you write the code.
| Unit | Relative to | Typical use |
|---|---|---|
| `rem` | Font size of the root (`html`) element | Font sizes, spacing, media query widths |
| `em` | Font size of the current element | Padding that should scale with that element's text |
| `%` | The parent's matching property | Widths inside a container |
| `vw` / `vh` | 1% of the viewport width or height | Full-bleed sections, fluid type |
rem: one scale for the whole page
Browsers default the root font size to 16px. The user can change this default in their browser settings. A length in rem follows that setting, while a length in px does not.
html {
/* Leave the root size alone so the user's setting applies. */
font-size: 100%;
}
body {
font-size: 1rem; /* 16px by default */
line-height: 1.5;
}
h1 {
font-size: 2rem; /* 32px by default */
margin-block-end: 1rem;
}Do not set html { font-size: 14px; }. That overrides the user's preference. If you want a smaller default, use a percentage such as 87.5%, which still scales with their setting.
em: scaling with the element
An em is the font size of the element where it is used. For font-size itself, it is the parent's font size. Padding in em grows when the text grows.
.button {
font-size: 1rem;
padding: 0.5em 1em; /* 8px 16px here; 12px 24px if font-size is 1.5rem */
}The common error is nesting. Because em font sizes compound, a list inside a list with font-size: 0.875em shrinks twice.
li { font-size: 0.875em; } /* A nested li becomes 0.875 × 0.875 = 0.77 of the base */Use rem for font sizes to avoid this. Keep em for padding and margins that should follow the element's own text.
Percentages and viewport units
A percentage resolves against the parent, so width: 50% means half of the parent's width. Percentage padding and margin resolve against the parent's width, even for vertical sides.
Viewport units resolve against the browser window. width: 100vw includes the vertical scrollbar width on some platforms, which can cause a horizontal scrollbar. Prefer width: 100% for full-width blocks. On mobile, 100vh can be taller than the visible area while the address bar is showing. Use 100dvh (dynamic viewport height) when a section must fit the visible screen.
.hero {
min-block-size: 100dvh;
padding: 1rem;
inline-size: 100%;
}Choosing a unit
Common errors
- Setting
html { font-size: 62.5%; }to make1remequal 10px. It works, but every unset size then becomes tiny, and third-party components break. - Using
pxfor media query widths. A query inemorremalso responds when the user raises their default text size, which keeps layouts from breaking at large text. - Using
vwalone for font size. The text cannot be zoomed, which fails accessibility guidance. Combine it withreminsideclamp(), covered in a later chapter.
Exercise
A .note has font-size: 1.25rem and padding: 0.5em. What is the padding in pixels with the default 16px root size, and what happens if the user raises their default size to 20px?
Show answer
The font size is 1.25 × 16 = 20px, so 0.5em padding is 10px. At a 20px root, the font size is 25px and the padding is 12.5px. Both scale together because the padding is relative to the element's own text.