Episode 1 — Fundamentals / 1.6 — CSS Core Fundamentals
1.6.e — CSS Units (px, rem, em, %, vw, vh)
In one sentence: CSS units fall into absolute (fixed physical/screen size) and relative (scaled to context) categories — choosing the right unit determines whether your design scales gracefully across devices, zoom levels, and user preferences.
Navigation: ← 1.6.d — Box Model · 1.6.f — Modern CSS Functions →
1. Absolute vs relative
| Category | Scales with | Examples |
|---|---|---|
| Absolute | Nothing — fixed size | px, cm, mm, in, pt |
| Relative | Some context (parent, root, viewport) | em, rem, %, vw, vh, vmin, vmax, ch, ex |
2. Unit-by-unit breakdown
px — CSS pixel
| Property | Detail |
|---|---|
| Type | Absolute |
| Relative to | Device pixel ratio (1 CSS px = 1/96th of an inch, but DPR adjusts) |
| Good for | Borders, shadows, thin lines — anything that should stay visually constant |
| Pitfall | Does not scale with user's font-size preference; avoid for typography and spacing |
rem — root em
| Property | Detail |
|---|---|
| Type | Relative |
| Relative to | :root (usually <html>) font-size |
| Good for | Typography, spacing, layout widths — scales with user preference |
| Why it matters | If a user sets their browser font-size to 20px, 1rem = 20px — your layout respects that |
html { font-size: 16px; } /* default in most browsers */
h1 { font-size: 2rem; } /* = 32px */
p { font-size: 1rem; } /* = 16px */
em — parent em
| Property | Detail |
|---|---|
| Type | Relative |
| Relative to | The element's own computed font-size (for most properties) or parent's font-size (when used on font-size itself) |
| Good for | Component-internal spacing that should scale with the component's text size |
| Pitfall | Compounding — nested em values multiply: 1.2em inside 1.2em = 1.44× root |
% — percentage
| Property | Detail |
|---|---|
| Type | Relative |
| Relative to | Parent's corresponding dimension (width: 50% = half of parent's width) |
| Good for | Fluid widths, responsive layouts |
| Pitfall | height: 50% only works if the parent has a defined height |
vw / vh — viewport units
| Unit | Relative to |
|---|---|
vw | 1% of viewport width |
vh | 1% of viewport height |
vmin | 1% of the smaller dimension |
vmax | 1% of the larger dimension |
Good for: full-bleed sections (height: 100vh), responsive typography scaling.
Pitfall — mobile vh: on mobile browsers, 100vh may exceed the visible viewport because of the address bar. Use the newer dynamic viewport units where supported:
| Unit | Meaning |
|---|---|
dvh | Dynamic viewport height (adjusts with address bar) |
svh | Small viewport height (always the smallest possible) |
lvh | Large viewport height (always the largest possible) |
ch — character width
1ch = width of the 0 glyph in the current font. Useful for constraining text measure:
.prose { max-width: 70ch; } /* ~70 characters per line — optimal readability */
3. Which unit when?
| Use case | Recommended unit | Why |
|---|---|---|
| Font sizes | rem | Respects user preference; scales globally |
| Spacing / padding | rem or em | Scales with text; consistent rhythm |
| Component-internal spacing | em | Scales with the component's own font size |
| Borders, shadows | px | Thin visual details should stay crisp |
| Fluid widths | % or vw | Responsive to container/viewport |
| Full-screen sections | dvh / vh | Viewport-relative height |
| Line width (measure) | ch | Content-aware readability |
| Media queries | em (or rem) | Scales with user zoom; px media queries don't |
4. The rem vs em decision
rem | em | |
|---|---|---|
| Reference | Root font-size (stable, global) | Element's own font-size (local, contextual) |
| Compounding | Never — always based on root | Yes — nested em values multiply |
| Best for | Global spacing, layout, typography | Padding/margins that should scale with the component's text |
Common pattern: use rem for everything by default; use em only for component padding/margins where you want spacing to grow/shrink with the component's font-size.
5. Accessibility and units
User font-size preferences (set in browser settings) adjust the :root font-size. If you use px for text, those preferences are ignored — the text stays fixed. Using rem (or em) respects the user's choice.
WCAG Success Criterion 1.4.4: Text must be resizable up to 200% without loss of content or functionality. Using relative units is the primary way to meet this.
6. Key takeaways
remis the go-to for typography and spacing — scales with user preference.emis useful inside components — but beware compounding.pxfor borders, shadows, and thin details — avoid for text.%andvw/vhfor fluid layouts — usedvhon mobile for viewport height.chfor readable line widths —max-width: 70chis a readability best practice.- Choosing the right unit is an accessibility decision — not just a design preference.
Explain-It Challenge
Explain without notes:
- Why
remrespects user font-size preferences butpxdoes not. - What em compounding is and how it can cause unexpected font sizes in nested elements.
- Why
100vhon mobile might not mean "the visible screen height" and whatdvhfixes.
Navigation: ← 1.6.d — Box Model · 1.6.f — Modern CSS Functions →