Values, Units and Math Functions

Beginner · 7 min

How to choose between px, rem, em, ch, percentages and viewport units, and how calc(), min(), max() and clamp() let one declaration adapt to its context.

Before thisThe Box Model and box-sizing

Kevin Powell · 7 min

Values, Units and Math Functions

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Absolute units (`px`) are fixed. Relative units resolve against something: `rem` against the root font size, `em` against the element's own font size (or its parent's, for `font-size` itself), `ch` against the width of a zero, `%` against the containing block.
  • Sizing text and spacing in `rem` respects the user's browser font-size setting, which `px` text does not.
  • `calc()` mixes units in one expression, such as `calc(100% - 2rem)`. `+` and `-` need spaces around them; `*` and `/` do not.
  • `min()` takes the smallest argument, `max()` the largest, and `clamp(min, preferred, max)` is shorthand for `max(min, min(preferred, max))`.
  • Viewport units (`vw`, `vh`) track the browser window, not any container. Mobile browsers add `svh`, `lvh` and `dvh` variants, covered in the responsive-design group.

Why there are so many units

A length could just be pixels. The reason CSS has a dozen units is that a page is shown on screens of unknown size, to people with different font settings, in languages with different character widths. A fixed 16px cannot adapt to any of that. Relative units are how a declaration says “this much compared to something”, and the browser works out the number once it knows the something.

Absolute: px

A CSS pixel is a fixed reference length, not a hardware pixel; on a high-density screen one CSS pixel covers several device pixels. It is the right unit for things that should not scale with text: hairline borders, box shadows, icon sizes tied to a bitmap.

Relative to text

  • rem: the root element’s font size. With the browser default of 16px, 1rem is 16px, and if a user raises their default to 20px, every rem value grows with it.
  • em: the element’s own computed font size. For the font-size property itself, it means the parent’s.
  • ch: the width of the 0 character in the element’s font. max-width: 65ch is a text measure that follows the font.
  • lh and rlh: the element’s and root’s line height. margin-block: 1lh is exactly one line of space.

I checked em and rem together in Chromium: with a root of 20px and a div at 10px, margin: 1em came out at 10px and padding: 1rem at 20px.

The trap with em is compounding. A font-size: 1.2em on nested lists multiplies at each level. Use rem for font sizes and em for spacing that should scale with the element’s own text, such as a button’s padding.

Relative to the layout

% resolves against the containing block, and which dimension it uses depends on the property: width: 50% is half the parent’s width, padding-top: 10% is 10% of the parent’s width too (as the box model lesson showed). The viewport units, vw, vh, vmin, vmax, are a percent of the window instead. 100vw includes the width of any visible scrollbar, which is a common source of unwanted horizontal scroll.

Math in a value: calc()

.sidebar { width: calc(100% - 2rem); }
.hero    { min-height: calc(100vh - 4rem); }
.stack   { padding: calc(1rem + 2vw); }

calc() is the one place where unlike units can be added, because the browser resolves each at layout time. Spaces around + and - are required. Multiplication needs at least one plain number, and division needs a plain number on the right. Browsers have also added round(), mod(), abs() and trigonometric functions.

Picking from a range: min(), max(), clamp()

min(90%, 400px) is 90% of the container, but never wider than 400px. max(10vw, 100px) is at least 100px. clamp(low, ideal, high) is both limits at once:

.card  { width: min(90%, 400px); }
.panel { width: clamp(200px, 50vw, 300px); }

I measured these in an 800px window: min(90%, 400px) gave 400px, max(10vw, 100px) gave 100px, and the clamp gave 300px, because 50vw is 400px, which exceeds the cap. clamp() replaces many media queries: one line gives a size that scales smoothly between a floor and a ceiling. It is the core of fluid typography.

Which unit for what

For Use
Font sizes rem
Padding that scales with its element’s text em
Text measure ch
Borders, shadows px
Fill a container %, or no width at all
Whole-screen sections viewport units, see viewport units and dynamic viewports
Spacing that adapts between a range clamp()

What the video covers

Kevin Powell’s “Are you using the right CSS units?” is about six and a half minutes and is from February 2021. By its title it is a guide to choosing a unit, and it is the closest match to the table above. I could not read a transcript, so the specific rules and measurements here are my own. The runner-up, from 2020, is longer and about min(), max() and clamp().

Where this fits

Everything that follows uses these units. Next, display and normal flow starts the layout group, and font families and web fonts opens typography, where rem, ch and lh do most of the work. The frontend roadmap’s responsive design lesson shows the media-query approach that clamp() often lets you skip.

Check yourself

  1. 01`html { font-size: 20px }` and `div { font-size: 10px; margin: 1em; padding: 1rem }`. What are the div's margin and padding?

    Margin 10px (`1em` of the div's own 10px font size) and padding 20px (`1rem` of the root's 20px). I measured these in Chromium.

  2. 02What does `width: clamp(200px, 50vw, 300px)` give in an 800px-wide window?

    300px. `50vw` is 400px, which is above the maximum, so the result is capped at 300px. Narrow the window to 500px and `50vw` is 250px, which sits in range, so that is the width.

  3. 03Why does `font-size: 1.5em` inside a parent with `font-size: 1.5em` make text grow twice?

    `em` for `font-size` is relative to the parent's font size, so the effects compound: 1.5 × 1.5 = 2.25 times the grandparent's size. `rem` always refers to the root, so it does not compound.

  4. 04Is `calc(100%-2rem)` valid?

    No. `+` and `-` inside `calc()` must have whitespace on both sides, because `-2rem` without a space reads as a negative number rather than a subtraction. Write `calc(100% - 2rem)`.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark