← Back to HTML & CSS, page 1

html-css-0016 · Easy fresher

How do px, em, rem, percentages and viewport units differ?

px is a fixed CSS pixel. em is relative to the element's own font size (the parent's for font-size itself), rem to the root font size, and percent to a property-dependent reference such as the containing block width. vw and vh are shares of the viewport. rem is predictable for text and spacing because it does not compound.

Full answer

Units say how big a length is. They fall into two groups: absolute and relative.

  • px is a fixed CSS pixel. It does not change with other settings on the page.
  • em is relative to the font size of the element itself. For the font-size property, it is relative to the parent's font size, which is why it compounds when nested.
  • rem is relative to the font size of the root element (html). It does not compound, so it is predictable.
  • % is relative to something that depends on the property. For width it is the containing block's width. For font-size it is the parent's font size. For padding and margin it is the containing block's width, even for the top and bottom sides.
  • vw and vh are 1 percent of the viewport width and height. ch is the width of the digit 0 in the current font, handy for line lengths.

Computed values in Chrome: 30px, 45px, 30px, 20px padding and a width of 200px for the 50% box.

Use rem for font sizes and spacing so the page respects the font size a user sets in the browser. Use em for things that should scale with their own text, such as button padding. Use px for hairlines such as a 1px border.

html
<style>
  html { font-size: 20px; }
  .parent { font-size: 1.5em; }         /* 30px */
  .child  { font-size: 1.5em; }         /* 45px: 1.5 x 30 */
  .rem    { font-size: 1.5rem; }        /* 30px: always 1.5 x 20 */
  .pad    { font-size: 10px; padding: 2em; } /* 20px: em uses its own font-size */
  .half   { width: 50%; }
</style>
<div class="parent" id="p">Parent
  <div class="child" id="c">Child (em)</div>
  <div class="rem" id="r">Child (rem)</div>
</div>
<div class="pad" id="pad">Padding in em</div>
<div style="width: 400px"><div class="half" id="half">Half</div></div>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • em compounds when nested; rem always refers to the root font size.
  • Percent depends on the property: width uses the containing block width, and padding and margin also use width.
  • vw and vh are 1 percent of viewport width and height.
  • rem respects a user's chosen default font size; px does not.
  • 100vw can include scrollbar width and cause horizontal scroll.

Common mistakes

  • using em for nested font sizes and getting sizes that grow at each level, such as nested lists.
  • using 100vw for full width. It includes the scrollbar width on systems with classic scrollbars, which can cause a small horizontal scroll.

Follow-ups

  • why does setting html { font-size: 62.5% } come up in tutorials?
  • Why do some people set html font-size to 62.5 percent?
  • What are dvh, svh and lvh for, and what should you check before using them?

Tips

  • Prefer rem for font-size. If you use px, a user who raised their default font size in the browser settings will not see the change.
  • Show one computed example, like 1.5em inside 1.5em giving 2.25 times the base size.
  • Say which unit you pick for font-size, borders and component padding, and why.