Skip to main content

CSS Foundations

CSS Units

Written by Updated CSS Units exercises

Choose lengths, percentages, angles, times, and resolution units according to what the value should respond to. It covers fixed, font-relative, viewport, container, percentage, time, angle, and resolution values with guidance on choosing the right relationship.

Every CSS length is relative to something. px is tied to the screen, rem to the reader's font setting, % to the parent, vw to the window. Pick the unit by asking what the size should follow.

So the useful question is not "which unit is best?" but "what should make this value grow or shrink?" Once you answer that, the choice is usually obvious. Units come up again in CSS Font Size and CSS Math Functions.

px, rem and em

  • px stays the same no matter what. Good for thin borders and small details.
  • rem follows the root font size, which follows the reader's browser setting. Use it for font sizes and spacing, so the whole page grows when someone needs bigger text.
  • em follows the font size of the current element. Use it when something should scale with its own text, like an icon next to a label.

Example

css

:root { font-size: 100%; }

.card {
  padding: 1.25rem;
  border-radius: 0.75rem;
}

.card__icon {
  width: 1.5em;
  height: 1.5em;
}

If the reader sets a bigger default font, the card's padding and corners grow with it, and the icon grows with the card's text.

Units that follow the screen or a container

vw and vh are one percent of the window's width and height. On phones, 100vh is often taller than what you can see, because of the browser's address bar. Use dvh (dynamic), svh (small) or lvh (large) instead when height matters. Inside a container query, cqw is one percent of the container's width.

Percentages depend on the property

A percentage is always "a percentage of something", and that something changes from property to property. A width in % is a share of the parent's width. A transform in % is a share of the element's own size. Padding in %, surprisingly, is always based on the parent's width, even for top and bottom.

Other units you will meet:

  • Angles: deg, rad, turn
  • Time: s, ms
  • Resolution: dpi, dppx
  • Grid fraction: fr
  • Unitless values: line-height, opacity, flex growth factors

Unit Reference

UnitRelative toTypical use
remRoot element's font sizeType and spacing
emElement's font sizeComponent-relative spacing
dvhDynamic viewport heightFull-height interfaces
cqwOne percent of a query container's widthContainer-responsive sizing

Complete Example

Complete runnable example

html

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>CSS Units example</title>
  <style>
    * { box-sizing: border-box; }
    body {
      margin: 0;
      padding: 2rem;
      font-family: system-ui, sans-serif;
      line-height: 1.5;
    }
    main { max-width: 70rem; margin-inline: auto; }
    article, section, aside, form, .card, .panel {
      padding: 1rem;
      border: 1px solid #cbd5e1;
      border-radius: 0.6rem;
    }

:root { font-size: 100%; }

.card {
  padding: 1.25rem;
  border-radius: 0.75rem;
}

.card__icon {
  width: 1.5em;
  height: 1.5em;
}
  </style>
</head>
<body>
  <main><article class="card"><span class="card__icon">★</span><h1>Relative units</h1><p>Resize the preview and change its root font size.</p></article></main>
</body>
</html>

Browser Support

Feature
Chrome
Edge
Firefox
Safari
rem unit4123.65
dvh, svh, and lvh units10810810115.4

Versions show the first stable desktop release with unprefixed support. Data source: MDN Browser Compatibility Data 8.0.8. A partial-support note is included where it changes how the example behaves.

A practical mix

Real pages use several units at once, and that is fine: px for borders, rem for type and spacing, em for icons, % or fr for layout. Choosing one favorite unit for everything is what causes problems.

CSS Units exercises

Pixels

Make the box 300px wide.

The HTML your code works on
<div class="box">
  <h2>Title</h2>
  <p class="text">Small print</p>
</div>

Result

Check your answer to see it here.