CSS Units Guide: px, rem, em, %, vw, vh, svh, ch and cqi

What each CSS unit is relative to, newer units like svh·dvh·ch·lh·cqi, and which unit to use for line-height and letter-spacing — with examples.

CSS units differ in what they are relative to: screen width, the user's font settings, or the parent element. This guide covers px, rem, em, % and vw·vh — what each is based on and how they differ — plus newer units like svh, dvh, ch, lh and cqi, and which unit fits line-height and letter-spacing, with examples.

1. Absolute units

1.1 Pixels (px)

px is the most common CSS length unit. Despite the name, it does not map 1:1 to physical device pixels. A CSS px is a "reference pixel" defined as 1/96 of an inch, so on high-density screens (device pixel ratio 2–3) one px is drawn as 2–3 device pixels. Zooming the browser scales px too, but unlike rem, increasing the browser's font size setting does not enlarge text sized in px — the biggest difference from rem.

.element {
  width: 100px;
  height: 50px;
  border: 1px solid #d0d0d0;
}

Characteristics of px

  • Fixed size: keeps the specified CSS pixel value regardless of screen width or font settings
  • Predictable: mockup values copy over directly
  • Ignores font settings: text in px does not grow when the user enlarges the browser font
  • No response to screen size: can cause horizontal scroll on narrow screens

Besides px, cm, mm, in, pt (1/72 inch) and pc are absolute units too, but they are rarely used on screen — they appear mainly in print styles (@media print).

2. Relative units

2.1 Viewport units (vw, vh)

Viewport units are relative to the browser viewport. 1vw is 1% of the viewport width, 1vh is 1% of the viewport height.

.responsive-container {
  width: 50vw;        /* 50% of the viewport width */
  height: 30vh;       /* 30% of the viewport height */
  font-size: 2vw;     /* 2% of the viewport width (use with care alone) */
}

Characteristics of viewport units

  • Proportional to width: grows by the same ratio as the screen gets wider
  • Keeps mockup proportions: reproduces a base-width design at the same ratio on any screen
  • Extreme values: can become too small or too large on very narrow or very wide screens
  • Zoom problem: text sized only in vw does not grow when the browser is zoomed

2.2 Root-relative units (rem)

rem is relative to the root element's (html) font size. With the browser default of 16px and no override, 1rem = 16px. If the user sets their browser font to 20px, 1rem becomes 20px too.

html {
  font-size: 100%;    /* browser default: usually 16px */
}

.element {
  font-size: 1.5rem;  /* 24px (16px × 1.5) */
  margin: 1rem;       /* 16px */
  padding: 0.5rem;    /* 8px (16px × 0.5) */
}

Characteristics of rem

  • Accessibility: follows the user's browser font-size setting (as long as the root font is not fixed in px)
  • Consistency: same reference everywhere, so nesting does not change the value
  • Maintainability: unify spacing and type in rem and manage the whole scale at once
  • Needs conversion: mockup px values must be divided by 16

2.3 Element-relative units (em)

em is relative to font size. In the font-size property it refers to the parent's font size; in margin, padding, width and other properties it refers to the element's own font size. Multipliers stack as elements nest, so use it carefully. To convert using the parent as the base, use the PX to EM converter.

.parent {
  font-size: 16px;
  padding: 1em;       /* 16px */
}

.child {
  font-size: 1.5em;   /* 24px (parent 16px × 1.5) */
  margin: 1em;        /* 24px (based on its own font size) */
}

/* A .child inside .child multiplies again: 16px → 24px → 36px → 54px */

Characteristics of em

  • Context-dependent: font-size uses the parent's font size; other properties use the element's own
  • Multipliers stack: nested em font sizes multiply and overshoot
  • Proportional spacing: ideal for button padding that should grow with the text
  • Hard to debug: you must trace every ancestor's font size to know the final value

2.4 Percent (%)

What % refers to depends on the property. For width, left and right it is the containing block's width (the parent's content area in normal flow); for height, top and bottom it is the containing block's height — and if the parent has no definite height, height % behaves like auto. Both padding and margin percentages are calculated from the containing block's width, even top and bottom. font-size % uses the parent's font size, line-height % the element's own, and transform: translate() % the element's own size.

.parent {
  width: 500px;
  height: 300px;
}

.child {
  width: 50%;         /* 250px (500px × 0.5) */
  height: 80%;        /* 240px (300px × 0.8) */
  padding-top: 10%;   /* 50px: 10% of the WIDTH (500px), not the height */
}

2.5 Other units worth knowing

  • svh / lvh / dvh: viewport height that accounts for the mobile address bar — smallest (svh, bar shown), largest (lvh, bar hidden) and the value that tracks the current state (dvh). Width equivalents svw, lvw, dvw exist too. Prefer 100svh or 100dvh over 100vh for full-screen sections.
  • vmin / vmax: 1% of the smaller (vmin) or larger (vmax) of the viewport's width and height.
  • ch: the width of the digit "0" in the current font. Use it to limit line length, e.g. max-width: 65ch. Korean glyphs are wider than "0", so fewer characters fit in the same ch value.
  • lh / rlh: the element's (lh) or root's (rlh) line-height. Handy for spacing that matches the text rhythm.
  • cqi / cqw: container query units — 1% of the nearest query container's inline size (cqi) or width (cqw), not the viewport.
.hero {
  min-height: 100svh;        /* never taller than the visible screen */
}

.article-body {
  max-width: 65ch;           /* about 65 "0" widths */
}

.square {
  width: 50vmin;             /* 50% of the shorter viewport side */
  aspect-ratio: 1;
}

.stack > * + * {
  margin-block-start: 1lh;   /* one line-height of space */
}

.card-wrap {
  container-type: inline-size;
}

.card-title {
  /* 18px at 300px container width, 24px at 600px */
  font-size: clamp(1rem, 0.75rem + 2cqi, 1.5rem);
}

Viewport units are covered in more depth in the viewport units guide.

2.6 Units for line-height and letter-spacing

Write line-height unitless, like 1.5. Values like 1.5em or 150% inherit the computed px value, so if a child's font grows, lines can overlap. A unitless value is recalculated against each element's own font size. For letter-spacing use em so it stays proportional to the font size — a mockup tracking of -2% is -0.02em.

body {
  font-size: 1rem;
  line-height: 1.5;          /* 16px × 1.5 = 24px */
}

h1 {
  font-size: 2rem;           /* 32px; inherited 1.5 recomputes to 48px line height */
}

.caption {
  letter-spacing: -0.02em;   /* -0.32px at 16px; ratio holds at any font size */
}

The line height calculator and letter spacing converter turn Figma values into CSS values directly.

3. Unit comparison

Unit Relative to Pros Cons Use for
px CSS reference pixel (1/96in) Predictable, matches the mockup Ignores font settings, no response to screen size Borders, shadows, max-width
vw Viewport width Proportional to width Breaks at extremes; 100vw includes the scrollbar Layout spacing, the proportional part of clamp()
vh Viewport height Proportional to height Affected by the mobile address bar Full-screen layouts (prefer svh·dvh on mobile)
rem Root font size Accessible, consistent Needs conversion from px Font sizes, padding, spacing
em Parent (font-size) or own font size Proportional to font size Multipliers stack when nested Spacing that should grow with text: button padding, icons, media queries
% Containing block (varies by property) Fits the parent The reference differs per property — confusing Widths, fluid layouts
svh·dvh Viewport height accounting for the address bar Accurate screen height on mobile dvh changes while scrolling Full-screen sections, modals

4. Practical guide

4.1 Choosing units for responsive layouts

/* Mobile first */
.container {
  width: 100%;              /* full width on mobile */
  max-width: 1200px;        /* cap on desktop */
  margin-inline: auto;
  padding: 1rem;            /* padding proportional to font size */
}

.card-list {
  display: grid;
  grid-template-columns: 1fr;   /* mobile: one column */
  gap: 1rem;
}

.card {
  padding: 1.5rem;
  font-size: 1rem;
}

/* Tablet and up */
@media (min-width: 48em) {      /* 768px */
  .card-list { grid-template-columns: repeat(2, 1fr); }
  .card { font-size: 1.125rem; }
}

/* Desktop */
@media (min-width: 64em) {      /* 1024px */
  .card-list { grid-template-columns: repeat(3, 1fr); }
  .card { font-size: 1.25rem; }
}

4.2 Accessible font sizing

When the user enlarges their browser font, rem values grow with it. There is no media query that detects the font-size setting itself, so the surest approach is simply not to override the root font with px.

/* Respect user settings: don't fix the root font in px */
html {
  font-size: 100%;  /* follows the browser default (usually 16px) and user changes */
}

/* Type scale (at a 16px root) */
.text-xs { font-size: 0.75rem; }   /* 12px */
.text-sm { font-size: 0.875rem; }  /* 14px */
.text-base { font-size: 1rem; }    /* 16px */
.text-lg { font-size: 1.125rem; }  /* 18px */
.text-xl { font-size: 1.25rem; }   /* 20px */
.text-2xl { font-size: 1.5rem; }   /* 24px */

5. Conversion formulas

5.1 px → vw

vw = (px ÷ base width) × 100

Example: 100px at a 1440px base
vw = (100 ÷ 1440) × 100 = 6.94vw

5.2 px → rem

rem = px ÷ root font size (16px by default)

Example: 24px to rem
rem = 24 ÷ 16 = 1.5rem

5.3 px → em

em = px ÷ reference font size (parent for font-size, own font size for other properties)

Example: 12px padding on a button with 16px text
em = 12 ÷ 16 = 0.75em

Media queries are based on the browser default font size (16px)
768px ÷ 16 = 48em

6. Principles for choosing units

  • Font sizes: use rem; keep the root font at 100%
  • Fonts that vary with screen: mix rem and vw as clamp(rem, rem + vw, rem)
  • Padding and spacing: use rem (consistency)
  • Spacing that should grow with text: use em (button padding, icon sizes)
  • Line-height and letter-spacing: unitless line-height, em letter-spacing
  • Layout widths: combine % with max-width; use 100% instead of 100vw for full width (avoids the scrollbar issue)
  • Full-screen heights: 100svh or 100dvh instead of 100vh
  • Body line length: limit with ch
  • Sizes inside components: container queries and cqi
  • Borders and shadows: use px (fixed size)
  • Media queries: use em. The em in a media query is based on the browser default font size (usually 16px), not html's font-size, so 768px becomes 48em. When users enlarge their font, layout breakpoints move with it.

Each principle with worked examples is collected in responsive CSS unit best practices.

7. Summary

In short: rem for fonts, em for spacing that must grow with text, % and max-width for layout widths, vw for values that should scale with the screen (preferably inside clamp()), and px for values that must not change, like borders. Convert mockup px values with the PX to VW converter, the PX to REM converter or the clamp generator.

Try it out right awayPX ↔ REM ConverterOpen →

Keep reading