# CSS Units: px, rem, em, %, viewport

> Lengths come in absolute, font-relative, viewport-relative, and percentage flavors. Knowing which base each resolves against is what makes sizing predictable.

Subject: CSS | Lesson 7 of 44 | 6 min read
Interactive lesson: https://codewithanimation.in/css/units-and-values
Previous: [The Box Model & box-sizing](https://codewithanimation.in/css/box-model-and-box-sizing.md) | Next: [calc(), clamp(), min() & max()](https://codewithanimation.in/css/calc-clamp-and-functions.md)

## Four families of length

Every CSS length is either **absolute** or **relative**. Absolute units (`px`, `pt`, `cm`, `in`) resolve to a fixed physical size. `px` is the only one you will use on screen; the others exist mostly for print. One `px` is defined as 1/96th of an inch, and browsers scale it on high-DPI displays so it stays visually consistent.

**Relative units** resolve against a base value, which is what makes them powerful for responsive and accessible design. The three sub-families are font-relative (`em`, `rem`, `ch`), viewport-relative (`vw`, `vh`, `dvh`...), and percentages. Each resolves against a different base, and mixing them up is the source of most sizing bugs.

## Relative units are how you respect the user

Browsers let users change their default root font-size (16px on most desktops, often larger for accessibility). If you size everything in `px`, your layout ignores that choice. Size text in `rem` and the whole document scales when the user bumps their default, which is the accessible default.

Viewport units let a value respond to the screen without a media query. `width: 90vw` is always 90% of the viewport. Modern `dvh`/`svh`/`lvh` units finally make full-height mobile layouts reliable by accounting for the browser's collapsing address bar.

## What each unit resolves against

**`rem`** resolves against the root element's (`:root` / `html`) font-size. Default is 16px, so `2rem` = 32px unless the root is overridden. Change one value (`html { font-size: 100%; }`) and every `rem` in the document follows. This is why `rem` is the recommended unit for font-size.

**`em`** has two behaviors and that is the whole trap. When `em` is used on `font-size`, it is relative to the PARENT's font-size. When `em` is used on any other property (padding, margin, width, border-radius...), it is relative to the ELEMENT's OWN computed font-size. So `padding: 1em` on an element with `font-size: 1.5em` gives padding equal to 1.5x the parent's font-size.

**Percentages** resolve against the parent's corresponding dimension: `width: 50%` is half the parent's width, `height: 50%` is half the parent's height. The exception that surprises everyone: `padding` and `margin` percentages are ALWAYS relative to the parent's WIDTH, even for `padding-top` and `margin-bottom`. This is why `padding-top: 50%` on a zero-height box creates a 2:1 aspect ratio.

**Viewport units:** `1vw` = 1% of viewport width, `1vh` = 1% of viewport height. `vmin`/`vmax` pick the smaller/larger of the two. The newer `svh` (small), `lvh` (large), and `dvh` (dynamic) viewport-height units fix the mobile browser bug where `100vh` was taller than the visible area because it ignored the address bar. Use `100dvh` for full-height mobile layouts.

**`ch`** is the width of the `0` character in the current font. It is the right unit for line-length limits: `max-width: 65ch` keeps paragraphs at a comfortable reading width regardless of font size.

**Unitless** values are not laziness, they are correct. `line-height: 1.5` is a multiplier applied to each child's own font-size, so it scales correctly inside nested elements. `line-height: 1.5em` would freeze the value at the parent's size. Always prefer unitless `line-height`.

## Common mistake: em compounding in nested components

When you set `font-size: 1.2em` on a list item, and that list contains nested list items also sized `1.2em`, each level multiplies: 1.2, then 1.44, then 1.728 times the root. The text balloons down the tree. This is the #1 reason `rem` is preferred for font-size: it always resolves against the single root, never compounds. Reserve `em` for spacing (padding, margin, gap) that should scale with the element's own text.

## Tip: Pick one unit per role and stick to it

A simple, robust convention: `rem` for font-size and media-query breakpoints, `em` for padding/margin/gap (so spacing scales with the element's text), `ch` for max line length, `%` for fluid widths inside a parent, and `px` only for hairline borders and tiny fixed details. Following this convention removes almost all unit confusion.

## Try it: Try it

Drag the root font-size and watch the `rem` bar rescale while the `px` bar stays fixed. See `em` compound when nested three levels deep.

The animation is on the lesson page and is not part of this text version: https://codewithanimation.in/css/units-and-values

## Check yourself

### Question 1

The root element has `font-size: 20px`. What is `2rem` in pixels?

- A. 40px
- B. 20px
- C. 32px
- D. It depends on the parent

Correct answer: A

`rem` = root em. Root font-size is 20px, so 2rem = 2 x 20px = 40px. `rem` ignores the parent chain entirely.

- Why not B: `2rem` means 2 times the root font-size. 2 x 20px = 40px, not 20px.
- Why not C: 32px would be `2rem` if the root were the browser default 16px. Here the root is 20px, so `2rem` = 40px.
- Why not D: `rem` always resolves against the root (`:root` / `html`), never the parent. That is the whole point of `rem` vs `em`.

### Question 2

A parent has `font-size: 16px`. Its child has `font-size: 1.5em` and `padding: 1em`. What is the child's padding in pixels?

- A. 24px
- B. 16px
- C. 1.5px
- D. 40px

Correct answer: A

The child's `font-size: 1.5em` resolves against the parent's 16px, giving the child a computed font-size of 24px. The `padding: 1em` then resolves against the child's OWN 24px font-size, so padding = 24px. This dual behavior of `em` is the key gotcha.

- Why not B: When `em` is used on `padding`, it resolves against the element's OWN computed font-size, not the parent's. The child's font-size is 1.5 x 16px = 24px, so `1em` of padding = 24px.
- Why not C: `em` is a multiplier on the font-size, not a literal 1.5px value. The child's font-size is 24px, so `1em` padding = 24px.
- Why not D: There is no `rem` here and no root override mentioned. The child's own font-size (24px) is the base for its `em`-based padding.

### Question 3

An element has `padding-top: 50%` inside a parent that is 400px wide and 300px tall. What is the padding-top in pixels?

- A. 200px
- B. 150px
- C. 400px
- D. It depends on the element's own size

Correct answer: A

Percentage values on `padding` and `margin` always resolve against the parent's WIDTH (the inline-size), regardless of which side they are applied to. 50% of the parent's 400px width = 200px. This rule is why `padding-top: 50%` creates a 2:1 aspect-ratio box.

- Why not B: 150px would be 50% of the parent's HEIGHT. But CSS percentage padding always resolves against the parent's WIDTH, even for `padding-top`.
- Why not C: 400px is the parent's full width, which would be 100% of the width. `padding-top: 50%` = 50% of 400px = 200px.
- Why not D: Percentage padding and margin always resolve against the PARENT's inline-size (width in horizontal writing mode), never the element's own dimensions.

## Remember this

- `px` is absolute (1/96th inch). `rem` = root font-size. `em` = parent font-size (for font-size) or own font-size (for spacing).
- `rem` for font-size, `em` for spacing, `ch` for line length, `%` for fluid widths, `px` for borders.
- Percentage `padding` and `margin` always resolve against the parent's WIDTH, on every side.
- Use `100dvh` (not `100vh`) for full-height mobile layouts to handle the address bar. Prefer unitless `line-height`.
