CSS units

Topic 4 of 4 in The box model8 min readBeginnerFree course

Overview

CSS has dozens of units, but you’ll use a handful most of the time. Choosing the right one makes layouts adapt naturally — to the screen, to the content and to the user’s own settings.

CSS units and what each is relative to. px: a CSS pixel, good for borders and fine details. rem: the root font size, usually 16px, good for type, spacing and most sizes. em: the element’s own font size, good for padding that scales with text. %: the parent, usually its width, good for fluid widths. vw and vh: 1% of the viewport width or height, good for full-screen sections. dvh: the viewport as it is right now, good for mobile heights. ch: the width of “0” in the font, good for readable line lengths. fr: a share of free space, for grid columns. Prefer rem for type and spacing so pages respect the user’s font-size setting.

Absolute: px

A CSS pixel (px) is a fixed unit — not necessarily one physical pixel (a phone might use three device pixels per CSS pixel), but always the same size on a given screen. Use it for things that shouldn’t scale with text: hairline borders, shadows, small fixed details.

CSS
border: 1px solid #d6d6d2;
box-shadow: 0 2px 8px rgb(0 0 0 / 0.1);

Other absolute units (cm, mm, in, pt) exist mainly for print stylesheets.

Font-relative: rem and em

rem (“root em”) is relative to the font size of the root <html> element — 16px by default. Crucially, the default comes from the user’s browser settings. Someone with low vision who sets their default font to 20px gets 1rem = 20px, and every rem-based size on your page grows with it.

CSS
h1 { font-size: 2.5rem; }          /* 40px by default, 50px for that user */
.section { padding-block: 4rem; }
.sidebar { width: 18rem; }

Sizes in px ignore that setting, so text in px doesn’t respect the user’s choice (browser zoom still works, but not the font-size preference). Use rem for font sizes, spacing and most layout sizes.

Don’t change the root font size to make maths easier (html { font-size: 62.5%; }) — it fights the user’s setting and complicates every component.

em is relative to the element’s own font size (or, for font-size itself, the parent’s). It’s ideal for things that should scale with the text they belong to:

CSS
.button {
  font-size: 1rem;
  padding: 0.75em 1.25em;       /* 12px × 20px; bigger buttons get bigger padding automatically */
  border-radius: 0.5em;
}
.button.large { font-size: 1.25rem; }   /* padding scales too */

Be careful with nested em font sizes — they compound: font-size: 1.2em inside another 1.2em is 1.44× the base. Using rem for font sizes avoids that.

Percentages

A percentage is relative to something in the containing block, usually its width:

CSS
.column { width: 50%; }           /* half the container's width */
.inset { padding: 5%; }           /* 5% of the container's WIDTH, even top/bottom */
.child { height: 50%; }           /* only works if the parent has a definite height */

Viewport units

Viewport units are relative to the browser window:

Unit 1 unit equals
vw / vh 1% of the viewport width / height
vmin / vmax 1% of the smaller / larger dimension
svh, lvh, dvh 1% of the small, large or dynamic viewport height
vi / vb 1% of the viewport in the inline / block direction

The mobile height problem

On phones, browser toolbars appear and disappear as you scroll, so the viewport height changes. Classic vh uses the largest possible height, so height: 100vh sections are partly hidden behind the toolbars when the page loads.

On a phone with the toolbars visible, the visible area is shorter; after scrolling, the toolbars hide and it’s taller. 100svh is the small viewport, with toolbars shown. 100lvh is the large viewport, with toolbars hidden. 100dvh is dynamic: whichever applies now.

The newer units fix this:

  • svh — the small viewport: toolbars visible. Content never hides behind them.
  • lvh — the large viewport: toolbars hidden (what vh usually means).
  • dvh — the dynamic viewport: updates as the toolbars show and hide.
CSS
.hero { min-height: 100dvh; }

dvh fits the screen exactly at all times, but it can make content resize while scrolling; svh is a stable alternative. These units are supported in all modern browsers.

A related trap: 100vw includes the vertical scrollbar on desktop, so width: 100vw can cause a horizontal scrollbar. Use width: 100% for full-width blocks instead.

Content-relative: ch and ex

ch is the width of the “0” glyph in the current font. It’s perfect for limiting line length:

CSS
.prose { max-width: 65ch; }        /* comfortable reading width */
input.postcode { width: 8ch; }     /* fits about 8 characters */

Grid: fr

fr is a fraction of the free space in a grid container — covered fully in Tracks and the fr unit.

Combining units

calc() mixes units, and min(), max() and clamp() choose between them:

CSS
.main { width: calc(100% - 18rem); }
.card { width: min(100%, 24rem); }
h1 { font-size: clamp(2rem, 1.25rem + 3vw, 3.5rem); }

Including a rem part in fluid font sizes (like 1.25rem + 3vw) keeps text responsive to zoom and font settings — pure vw text doesn’t grow when users zoom.

Quick guide

For… Use
font sizes rem (or clamp() with rem)
spacing and most sizes rem
padding inside buttons and badges em
fluid widths %, fr, or nothing at all (auto)
line lengths ch
full-screen sections dvh / svh with min-height
borders, shadows px

Respect the user: if you only remember one thing from this lesson, size text in rem, not px.

Exercise

A full-height mobile hero

Build a .hero section that fills the visible screen on phones without being hidden behind the browser toolbars (use the right viewport unit), has padding of 1.5rem, and a heading whose size is set in rem. Add a .button whose horizontal padding is 1.25em and vertical padding is 0.75em, so it scales if you change its font-size. Then test: change the browser’s default font size (Settings → Appearance → Font size) and check that the heading and button grow.

HTML
<section class="hero">
  <h1>Learn CSS layout</h1>
  <a class="button" href="#start">Start now</a>
</section>

<style>
  * { box-sizing: border-box; }
  body { margin: 0; }
  /* your code here */
</style>

Summary

  • px is an absolute CSS pixel; rem is relative to the root font size and respects user settings.
  • em is relative to the element’s own font size — good for padding that scales with text.
  • Percentages are relative to the containing block, usually its width.
  • vw/vh are 1% of the viewport; on mobile prefer dvh, svh or lvh over vh.
  • Use ch for line lengths, fr for grid tracks, and combine units with calc(), min(), max() and clamp().