CSS has around fifteen units, and picking the wrong one is a common cause of layouts that break when somebody changes a setting somewhere else.
The good news is that four of them cover almost every case, and each one has a clear job.
The previous lesson in this module covered "What Does Inheritance Pass Down and What Does It Not?", and the next lesson in this module is about "How Do Colors, Opacity and Contrast Work in CSS?".
What are the four units that you actually need?
.fixed-width { width: 320px; } /* absolute: exactly this many pixels */
.relative { font-size: 1.25rem; } /* relative to the root font size */
.em-size { padding: 0.5em; } /* relative to this element's font size */
.percent { width: 50%; } /* relative to the parent */The px is absolute and it is predictable, which makes it right for things that must not move. That is a border width, an icon size, or a minimum tap target.
The rem is relative to the root font size, which makes it right for anything that should scale with the reader's preference.
The em is relative to the element's own font size, which makes it right for spacing that should follow the text.
And percentages are relative to the containing block, which makes them right for filling a parent.
What is the difference between the rem and the em?
Both are relative to a font size, but the font size they relate to is different.
The rem means "the root element's font size". That is the size that is set on the html.
The em means "this element's own font size", which for most properties means the inherited one, but for the font-size property itself means the parent's.
html { font-size: 16px; }
body { font-size: 1rem; } /* 16px */
h1 { font-size: 2rem; } /* 32px — always relative to the root */
.card { font-size: 20px; }
.card h2 {
font-size: 1.5em; /* 30px — relative to the card's 20px */
margin-bottom: 0.5em; /* 15px — relative to the heading's 30px */
}That double meaning of the em on margins is a classic trap. A heading with a font size of 30px and a margin of 0.5em gets the 15px margin and not the 8px, because the margin resolves against the element's own font size.
It is sometimes what you want, and it is confusing enough that many teams use the rem for spacing as a rule.
When should you use percentages?
Percentages are relative to the containing block, and that is a strength for width and height. It is a weakness for padding and margin, where the reference is the containing block's width even for a vertical property.
.card {
width: 60%; /* 60% of the parent's width */
padding: 5%; /* 5% of the parent's WIDTH, not its height */
}That asymmetry is why padding: 5% is a trap. The padding changes with the width of the parent, so the same card looks different in a wide column and in a narrow one.
If you want padding that is proportional to the element's own size, then you use the em instead.
What are the viewport units for?
The vw, the vh, the vmin, and the vmax are relative to the size of the viewport, and their main job is full-height layouts, which are otherwise awkward.
/* a section that fills the screen */
.hero {
min-height: 100vh;
display: grid;
place-items: center;
}
/* never taller than the visible area, even on a phone with a browser bar */
.hero {
min-height: 100svh;
}The svh, the lvh, and the dvh variants exist because the viewport height changes on a phone as the browser bar hides and shows.
The small viewport height is the safest choice for a full-screen layout, because it is the height that is always visible.
Why does the root font size matter?
Because it is the user's preference expressed as a setting.
A reader who has set a larger default font size in the browser has told the browser to make the text larger, and any layout that ignores that preference is overriding their choice.
/* do this: let the reader decide the base size */
html {
font-size: 100%;
}
body {
font-size: 1rem;
}
/* avoid this: it ignores the reader's preference */
body {
font-size: 16px;
}With the font-size: 100% on the root, and everything else in the rem, the whole layout scales with the reader's preference. That includes the spacing, because the spacing is in the rem too.
That is the practical argument for the unit. It is not that pixels are wrong, but that they are a promise you cannot keep for somebody else.
How do you size something fluidly?
Fixed units give you predictable results and inflexible layouts.
Percentages give you fluid results and awkward ones, because a percentage of a percentage needs a parent to resolve against.
The modern answer is to let a value sit between two bounds, and to express the fluid part in viewport units.
The function you want is the clamp.
It takes a minimum, a preferred value, and a maximum, and it picks the first one that fits.
.headline {
/* never smaller than 1.5rem, never larger than 3rem, fluid in between */
font-size: clamp(1.5rem, 1.1rem + 2vw, 3rem);
}
.card {
/* fluid width with a floor and a ceiling */
width: clamp(16rem, 90vw, 28rem);
}The middle term is where the fluidity comes from.
A vw is one percent of the viewport width, so the 1.1rem + 2vw grows as the window widens.
Adding a fixed rem part keeps the text from shrinking below a readable size on a narrow screen, and the two clamp bounds stop it from growing without a limit on a wide one.
The result is a headline that responds to the viewport without a single media query, and without the sudden jumps that a breakpoint produces.
The rem in the middle term is deliberate. It is what the user changes when they increase the browser font size, so the fluid part scales with the user's preference and not against it.
The same idea works for spacing. A container that is the width: min(100% - 2rem, 70rem) is fluid until it hits the maximum width, and the min() function picks whichever of the two values is smaller. That is exactly the behaviour you want without needing a breakpoint.
Quick reference
- The
pxfor things that must not move. That is borders, icons, and small fixed elements. - The rem for anything that should follow the reader's font size preference.
- The
emfor spacing that should follow the element's own text size. - Percentages fill the parent, and percentage padding is based on the parent's width.
- The
100svhis the safe choice for a full-height section on a phone. - You set the root font size in %, and not in
px, so that the reader's preference is respected.
Try it yourself: build a card with a heading and a paragraph, and then change its font size and watch which surrounding values move with it. The em margin moves, and the rem margin does not.
Then set the default browser font size to something larger and reload. The rem layout scales, and the layout that is built in px does not.