Units

An overview of length units in CSS: from fixed pixels, through font-relative em and rem, to viewport, container, and grid units.

On this page

Example

#
CSS
.lesson {
  max-width: 45ch;
  padding: 1rem 1.25rem;
  border: 1px solid #3f3f46;
  border-radius: 0.75rem;
}

.lesson h3 {
  margin: 0 0 0.5rem;
  font-size: 1.25rem;
}

.lesson .tag {
  display: inline-block;
  margin-bottom: 0.5rem;
  padding: 0.25em 0.75em;
  border-radius: 1em;
  background: #3b0764;
  font-size: 0.75rem;
}
Preview

Each unit has its job here: ch limits the text width, rem keeps spacing and sizes consistent, em scales the label with its font, and px draws a border that should always be thin.

Definition and usage

#

Every length in CSS is a number with a unit, for example 16px, 1.5rem, or 50%. The exception is zero, for which the unit can be omitted. Units are divided into absolute ones, which always mean the same (in practice only px), and relative ones, computed from something else: the font size, the parent, the browser window, or a container.

em and rem depend on the font. 1rem is the font size of the root element <html> (16px by default), the same throughout the document. 1em is the font size of the current element, so it changes along with it, and in the font-size property itself it is computed from the parent’s font. That is why em compounds in nested elements, and rem doesn’t.

Viewport units are computed from the viewport: 1vw is 1% of its width, 1vh 1% of its height. On phones the window height changes when the address bar hides, which is why variants appeared: svh (small viewport, bar visible), lvh (large viewport, bar hidden), and dvh (dynamic, changes continuously). For full-screen sections, 100svh or 100dvh are better than 100vh.

There are also units for special tasks. ch is the width of the character “0” in the current font, so it is good for limiting line length. fr works only in CSS Grid and means a share of the free space. cqi is 1% of the width of a container with container-type. Percentages are usually computed from the parent, but whether from its width or height depends on the property: width from the width, height from the height, and padding and margin always from the width.

Syntax

#
Syntax
<number><unit>

absolute: px
font-relative: em, rem, ch
relative to parent, viewport, or container: %, vw, vh, svh, lvh, dvh, cqi
grid: fr

Units

#
  • px

    a fixed value

    The CSS pixel is an absolute unit. The tile is always 160 px, regardless of the parent and the window. On high-density screens one CSS pixel is several physical pixels, so sizes look similar on every device.
  • em

    the element’s font

    Spacing and rounding in em grow with the button’s font. Just change font-size and the proportions are preserved.
  • rem

    the html element’s font

    Both labels are 20 px (1.25 × 16 px), even though their parents have different fonts. rem is always computed from the <html> element, so it doesn’t compound.
  • %

    the parent’s size

    Percentages are computed from the parent. The bars are a quarter, a half, and the whole width of the dashed frame.
  • vw / vh

    window width and height

    50vw is half the window width, here of the preview frame, not of the dashed box, which is why the first tile is wider than half of the box. 50vh is half the window height.
  • dvh / svh / lvh

    window height on a phone

    On a phone the address bar hides and shows. svh computes the height with the bar visible (the smallest), lvh with it hidden (the largest), and dvh changes continuously. The preview frame has no bar, so all three tiles are equal.
  • ch

    width of the 0 character

    The width of the character “0” in the current font. The paragraph is no wider than 24 such characters, and the postcode field fits exactly as much as needed.
  • fr

    free space in a grid

    fr works only in a grid and means a share of the free space. The middle column gets two shares, and the side ones one each.
  • cqi

    container width

    Drag the frame’s bottom-right corner. The text is 9% of the container width, so it grows and shrinks with it, not with the window.

Units in practice

#

px

The CSS pixel is an absolute unit. The tile is always 160 px, regardless of the parent and the window. On high-density screens one CSS pixel is several physical pixels, so sizes look similar on every device.
.demo { width: 160px; }

em

Spacing and rounding in em grow with the button’s font. Just change font-size and the proportions are preserved.
.demo {
  padding: 0.5em 1em;
  border: 0;
  border-radius: 0.5em;
  background: #7c3aed;
  color: #fff;
}

rem

Both labels are 20 px (1.25 × 16 px), even though their parents have different fonts. rem is always computed from the <html> element, so it doesn’t compound.
.demo {
  font-size: 1.25rem;
  font-weight: 700;
  color: #a78bfa;
}

%

Percentages are computed from the parent. The bars are a quarter, a half, and the whole width of the dashed frame.
.a { width: 25%; }
.b { width: 50%; }
.c { width: 100%; }

vw / vh

50vw is half the window width, here of the preview frame, not of the dashed box, which is why the first tile is wider than half of the box. 50vh is half the window height.
.vw { width: 50vw; }
.vh { height: 50vh; }

dvh / svh / lvh

On a phone the address bar hides and shows. svh computes the height with the bar visible (the smallest), lvh with it hidden (the largest), and dvh changes continuously. The preview frame has no bar, so all three tiles are equal.
.s { height: 50svh; }
.d { height: 50dvh; }
.l { height: 50lvh; }

ch

The width of the character “0” in the current font. The paragraph is no wider than 24 such characters, and the postcode field fits exactly as much as needed.
.demo { max-width: 24ch; margin: 0 0 10px; }
.zip { width: 8ch; font-family: monospace; }

fr

fr works only in a grid and means a share of the free space. The middle column gets two shares, and the side ones one each.
.demo {
  display: grid;
  grid-template-columns: 1fr 2fr 1fr;
  gap: 8px;
}

cqi

Drag the frame’s bottom-right corner. The text is 9% of the container width, so it grows and shrinks with it, not with the window.
.cq { container-type: inline-size; }
.cq p { font-size: 9cqi; }

More examples

#
A section as tall as a phone screen
CSS
.hero {
  min-height: 100vh;
  min-height: 100svh;
}

The first declaration is a fallback for older browsers, the second overrides it where svh works. The section fills the phone screen and its bottom doesn’t hide under the address bar. This example has no preview, because the frame fits its height to the content.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Give font sizes in rem, not px. The text then responds to the font size setting in the browser, which people with low vision rely on.
  • Beware of 100vw: it also includes the vertical scrollbar, so an element can extend beyond the page and cause horizontal scrolling. For full width 100% is usually enough.
  • Limit the line length, for example max-width: 65ch. Lines of a few dozen characters are the most comfortable to read.

Related entries

#

Spotted a mistake or missing an example? Write to us.