Skip to content
Tutorials Center
My learning

Topics

Lesson 20 of 139 ยท Values and Units

Units: px, em, rem and More

Learn the difference between absolute and relative CSS units, when to use px, em, rem, %, ch and lh, and how em compounds while rem stays predictable.

Almost every CSS layout decision involves a length: a font size, a gap, a width, a border. CSS gives you more than a dozen units to write those lengths in, and choosing the right one makes the difference between a page that adapts gracefully and one that breaks the moment a user zooms or changes their default font size.

The good news is that you only need a handful in daily work. This lesson sorts the units into two families, shows exactly what each relative unit is relative to, and ends with a practical cheat sheet for picking one.

Lengths, numbers and zero

A length is a number followed immediately by a unit: 16px, 1.5rem, 40%. There is no space between them. 16 px is invalid, and the browser drops the whole declaration.

A few rules to remember:

  • Zero needs no unit. margin: 0 is the same as margin: 0px.
  • Some properties take plain numbers, without a unit: line-height: 1.5, opacity: 0.8, z-index: 10, flex-grow: 1. Adding a unit there is either invalid or changes the meaning.
  • Negative values are allowed where they make sense, such as margin: -8px, but not for padding or width.

Absolute units

Absolute units have a fixed size that doesn't depend on anything else on the page.

Unit Name Equals
px pixel 1/96 of an inch
pt point 1/72 of an inch (about 1.33px)
in inch 96px
cm centimeter about 37.8px
mm millimeter about 3.78px
Q quarter-millimeter about 0.94px
pc pica 12pt, or 16px

A CSS pixel is not a hardware pixel. On a high-density phone screen, one CSS pixel may cover three or four physical pixels in each direction. CSS defines its pixel as a viewing-angle unit, so 1px looks roughly the same size on any device held at a normal distance. The same goes for in and cm: they are locked to the CSS pixel, so a 1in box won't measure exactly one inch on most screens.

On screen, px is the only absolute unit you need. pt, cm and mm are useful in print styles, where physical sizes mean something.

Relative units

Relative units are measured against something else: a font size, the parent's width, or the viewport. They let designs scale.

em: relative to the current font size

1em equals the element's own computed font-size. If a button has font-size: 20px, then padding: 0.5em is 10px. Make the font bigger and the padding grows with it, which keeps the button's proportions.

There is one exception: when you use em in the font-size property itself, it is relative to the parent's font size (the element doesn't have its own yet). And that causes the famous compounding problem.

em compounds, rem does not

In the em column every nested <li> is 85% of an <li> that is already 85%, so the text keeps shrinking: 13.6px, 11.6px, 9.8px, 8.4px. In the rem column every level is 13.6px.

rem: relative to the root font size

1rem equals the font-size of the root element, <html>. Unless you change it, that's the browser's default, which is 16px in every major browser. But users can change that default in their browser settings, often because they need larger text. When they do, everything sized in rem grows with it; everything in px stays put.

That's the main reason to size text in rem:

CSS
html { font-size: 100%; }      /* respect the user's setting; don't force 16px */
body { font-size: 1rem; }
h1   { font-size: 2.25rem; }
small { font-size: 0.875rem; }

Avoid html { font-size: 62.5% } tricks that make 1rem equal 10px "for easy math". They work, but every component then has to reset its text size, and any third-party code that assumes 1rem = 16px breaks. Just think in sixteenths: 0.75rem is 12px, 1.25rem is 20px.

Percentages: relative to "something", depending on the property

A percentage always refers to some other value, but which value depends on the property:

Property % is relative to
width, margin, padding (all sides!) The containing block's width
height The containing block's height (only if that height is definite)
font-size The parent's font size
line-height The element's own font size
translate, transform: translate() The element's own size

The padding row surprises people: padding-top: 50% is half the parent's width, not its height. The height row explains the classic "height: 100% does nothing" bug: if the parent's height is auto, there is nothing definite to take a percentage of.

ch, ex, lh and other font-relative units

  • ch is the width of the digit "0" in the current font. It's perfect for measuring text: max-width: 65ch gives roughly 65 to 75 characters per line, a comfortable reading measure (see Line Height and Vertical Rhythm). It's also great for inputs sized for their content, like width: 6ch for a postal code.
  • ex is the x-height of the font, the height of a lowercase "x". It's handy for nudging small icons to line up with text.
  • lh is the element's computed line height, and rlh is the root's. margin-block: 1lh gives a gap of exactly one line. These became available in all major browsers in 2023.
  • cap (cap height) and ic (the width of a CJK ideograph) exist too, but you'll rarely need them.
ch and lh for text-shaped sizes

Change the body font size to 20px and watch the article get wider: the line still holds about the same number of characters.

Viewport and container units

Units such as vw, vh and dvh are relative to the browser window. They have enough quirks to deserve their own lesson: Viewport Units. Container query units (cqi, cqb) do the same for a parent container; you'll meet them in Container Queries.

Seeing units side by side

This demo draws bars of the same "number" in different units, so you can see how they relate. Use the slider to change the parent's font size: the em and ch bars react, the rem and px bars don't.

Units comparison

The 40% bar depends on neither font size: it is 40% of the row's width, so it changes only when the preview gets wider or narrower.

Which unit should you use?

There is no single right answer, but these defaults serve well:

Use case Good choice Why
Font sizes rem Respects the user's font size setting; no compounding
Padding and gaps inside components em or rem em scales with the component's text; rem stays consistent
Text line length ch Follows the font
Layout widths %, fr, or none at all Let flexbox and grid share the space
Borders, hairlines, shadows px Should stay crisp and thin at any text size
Line height a unitless number Scales with each element's font size
Media query breakpoints em or rem Breakpoints move with the user's font size

Two habits matter most: never size body text in px alone, and let layout widths be fluid instead of fixed. The CSS functions lesson shows how clamp() combines units to get fluid sizes with sensible limits.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. A <p> has font-size: 20px. What is its padding: 0.5em?

    Outside font-size, em uses the element's own computed font size: 0.5 ร— 20px = 10px.
  2. 2. Why does nested text keep shrinking with li { font-size: 0.8em }?

    Each nested <li> takes 80% of a parent that is already 80%, so the sizes compound. rem avoids this.
  3. 3. What is padding-top: 10% relative to?

    Percentage margins and padding on every side are measured against the containing block's width.
  4. 4. Which unit is best for limiting a paragraph to a comfortable reading width?

    ch is based on the width of the font's characters, so max-width: 65ch keeps the measure readable whatever the font size.
  5. 5. Why are rem font sizes better for accessibility than px font sizes?

    The root font size starts from the user's browser setting, so rem-based text grows when a user asks for bigger text.

Summary

  • Write lengths as a number plus a unit with no space; zero needs no unit, and some properties take plain numbers.
  • px is the screen's absolute unit; a CSS pixel is a reference size, not a hardware pixel. Physical units belong in print styles.
  • em is relative to the element's font size (the parent's, inside font-size), so it compounds when nested.
  • rem is relative to the root font size, which follows the user's setting: the best default for text.
  • Percentages depend on the property; padding and margins use the containing block's width.
  • ch, ex and lh size things relative to the text itself.

Next: Viewport Units โ€” sizing things relative to the browser window, including the mobile address bar problem.