Sub topic 3
Thinking in relative units
This chapter assumes you know CSS selectors, the box model and the cascade, and that you can inspect computed styles in browser developer tools. It covers how the main relative units work, where each one fits in a mobile-first stylesheet, and the mistakes that cause most unit bugs.
Why relative units suit mobile-first CSS
An absolute unit such as px is a fixed amount in CSS pixels. A relative unit is a multiple of something else: the root font size, the current font size, a parent box, or the viewport. A mobile-first stylesheet has to work on a 320px phone, a tablet, a desktop monitor, and a phone whose owner raised the default text size. Fixed values cannot track those changes. Relative values do.
Each relative unit has a reference point, and most unit bugs come from using the wrong one. The sections below name the reference for each unit.
| Unit | Relative to | Typical use |
|---|---|---|
| `rem` | Font size of the root (`html`) element | Font sizes, spacing scale, media queries |
| `em` | Font size of the current element | Padding and radii inside a component |
| `%` | Depends on the property (see below) | Widths, fluid boxes |
| `vw`, `vh` | 1% of the viewport width or height | Large display text, full-height sections |
| `ch` | Width of the "0" glyph in the current font | Line length |
rem: the root-relative unit
One rem equals the computed font size of the html element. Browsers default that to 16px, but users can change it in their settings. Sizing text and spacing in rem means your layout follows that choice.
html {
font-size: 100%;
}
body {
font-size: 1rem;
line-height: 1.5;
}
h1 {
font-size: 2rem;
margin-block: 1.5rem 0.75rem;
}
.container {
max-inline-size: 60rem;
padding-inline: 1rem;
}Setting html to 100% keeps the user's preferred size. Avoid html { font-size: 16px; }. A pixel value overrides the browser setting, and every rem in your stylesheet then ignores the user's preference.
Some developers set html { font-size: 62.5%; } so that 1rem equals 10px, which makes the arithmetic easy. It works, but you then have to restore body text with font-size: 1.6rem. Any third-party component that assumes the default size will look too small. It is simpler to learn that 1rem is 16px by default and 0.5rem is 8px.
em: relative to the current element
An em is the font size of the element it is used on. There is one exception. When you use em on the font-size property itself, it refers to the parent's font size, since the element's own size is the thing being computed.
That behavior is useful for components that should scale as a unit:
.button {
font-size: 1rem;
padding: 0.5em 1em;
border-radius: 0.25em;
}
.button.large {
font-size: 1.25rem;
}The large button's padding and radius grow along with its text because they are written in em. You change one value and the whole component scales proportionally.
The compounding trap
Because em on font-size depends on the parent, nested elements multiply:
ul {
font-size: 0.8em;
}With nested lists, the first list is 80% of its parent, and the inner list is 80% of that, which is 64% of the original. A third level is 51%. The text shrinks at each level.
ul {
font-size: 0.8rem;
}Using rem for font-size removes the multiplication, because every list is measured against the root. A good rule: use rem for font-size, and use em for other properties (padding, margin, border radius) that should track the element's own text size.
Percentages: the reference depends on the property
A percentage is not a single unit with a single meaning. Each property defines what it is a percentage of.
| Property | Percentage of |
|---|---|
| `width`, `max-width` | Width of the containing block |
| `height` | Height of the containing block, only if that height is set explicitly |
| `padding`, `margin` (all four sides) | Width of the containing block |
| `font-size` | Parent's font size |
| `line-height` | The element's own font size |
| `translate()` in `transform` | The element's own box size |
Two rows surprise people. First, padding-top: 50% is half the container's width, not height. Second, height: 100% does nothing if the parent's height is auto, because the parent's height depends on its children and the browser cannot resolve the circle.
In a mobile-first stylesheet, percentage widths work well for fluid boxes:
.sidebar {
inline-size: 100%;
}
.thumbnail {
inline-size: 50%;
max-inline-size: 12rem;
}The max-inline-size in rem caps the percentage so the thumbnail does not grow without limit on a wide screen. Combining a relative preferred size with a capped maximum is a pattern you will reuse often.
Viewport units
1vw is 1% of the viewport width, and 1vh is 1% of its height. They respond to the window rather than any parent, so they suit things that should fill the screen.
.hero {
min-block-size: 100svh;
padding: 2rem 1rem;
}Mobile browsers complicate vh. The address bar slides away as the user scrolls, so the visible height changes. Classic 100vh is measured as if the bar were hidden, which pushes content off the bottom on first load. Three newer variants fix this:
svhis the small viewport height, with browser toolbars fully shown.lvhis the large viewport height, with toolbars hidden.dvhis the dynamic viewport height, which updates as the toolbars move.
svh is the safest choice for a minimum height. dvh tracks changes but can cause the layout to resize while scrolling.
The scrollbar gotcha
100vw includes the width of a classic vertical scrollbar. On a desktop browser with a visible scrollbar, width: 100vw is wider than the space available, and you get a horizontal scrollbar. For full-width blocks, use inline-size: 100% or leave the block at its default auto width.
Also avoid sizing body text in vw alone. Text sized only by viewport width ignores the user's font setting, which fails the zoom requirements in accessibility guidelines. The chapter on fluid typography shows how to mix rem with vw safely.
ch and unitless line height
The ch unit is roughly the width of the "0" character in the current font. It is useful for setting a readable line length:
.prose {
max-inline-size: 65ch;
}For line height, prefer a unitless number such as line-height: 1.5. A unitless value is inherited as a multiplier, so each descendant calculates it from its own font size. A value like 1.5em is computed once on the parent and inherited as a fixed length, which can make child text with a larger font overlap.
Media queries use a different reference
Inside a media query, em and rem both mean the browser's initial font size, usually 16px. Your own html rule does not affect it. So @media (min-width: 48em) is 768px at the default, and it moves if the user raises the default text size. Layout then changes earlier, which gives larger text more room. Pixel breakpoints cannot do this.
Choosing a unit
Common errors
- Setting
html { font-size: 16px; }and overriding the user's preference. - Using
emforfont-sizein nested elements, so sizes shrink or grow at every level. - Expecting
height: 100%to work when the parent has no set height. - Using
width: 100vwand getting a horizontal scrollbar. - Using
100vhfor a full-screen section on mobile and losing the bottom of the content behind the browser toolbar. - Using
1.5emfor line height and getting inherited fixed lengths.
Exercise
A nested <ul> inside another <ul> renders in noticeably smaller text than the outer one. The stylesheet has ul { font-size: 0.9em; }. Explain the cause and give a fix.
Show answer
In font-size, em is relative to the parent's font size, so the inner list is 90% of the outer list's size, which is already 90% of its parent. The sizes multiply at each nesting level. Change the rule to ul { font-size: 0.9rem; }. A rem value is measured against the root element, so every list gets the same size.
Exercise
You write .banner { width: 100vw; } and a horizontal scrollbar appears on a desktop browser that shows a vertical scrollbar. Why, and what do you write instead?
Show answer
100vw includes the width of the vertical scrollbar, so the banner is wider than the space the page has for content. Use inline-size: 100%, or remove the width declaration so the block takes the available width by default.