Skip to content

Menu

CSS Units Cheat Sheet: rem, em, Viewport, Container Units and clamp()

Checked against CSS CSS Values and Units Level 4 (W3C Working Draft, 12 Mar 2024) and MDN as of Nov 2026 on 28 November 2026. Each section links to the official docs it was checked against.

This sheet is for front-end developers who keep second-guessing which unit to type. It follows the CSS Values and Units Level 4 draft and the MDN reference pages as they read on 28 Nov 2026. The px conversions were worked out by hand from the spec's ratios; nothing here needs a browser flag in current Chrome, Firefox or Safari, but rows I only read about are marked untested.

When to use what

Size text with rem. It follows the root font size, which is the one number a user changes in browser settings, so your layout respects that choice for free. Use em only when something should scale with its own text, like padding inside a button or an icon next to a label. Nesting em font sizes compounds, and that is the bug people remember it for.

Use px for things that should stay crisp and fixed: hairline borders, shadows, focus outlines. A CSS pixel is defined as 1/96 of an inch, not a device pixel, so it already scales on high-density screens.

For full-height mobile layouts, stop reaching for 100vh. Plain vh equals the large viewport, measured as if the browser toolbars are hidden, so content can sit behind the address bar. 100svh always fits; 100dvh tracks the toolbar as it slides, which can cause relayout while scrolling. I default to svh for heroes and use dvh only for full-screen app shells.

Inside reusable components, prefer container units (cqi) over viewport units, because the component should react to the box it lives in, not the window. And for fluid type, clamp() with rem bounds beats a stack of media queries; the responsive patterns post shows both ideas in real layouts. Our CSS unit converter does the px, rem and em arithmetic, and the CSS units explained post goes deeper on the reasoning.

The ratios come from the spec's table at https://www.w3.org/TR/css-values-4/ (1in = 96px). On screens these are not real physical inches; only print gets close.

1px

One CSS pixel, defined as 1/96 of an inch; a high-density screen paints it with several device pixels

1in = 96px

One inch, and the anchor every other absolute unit is derived from

1cm = 96px / 2.54

One centimetre, about 37.8px; useful in print stylesheets

1mm = 1cm / 10

One millimetre, about 3.78px

1Q = 1cm / 40

A quarter millimetre, about 0.94px

1pt = 1in / 72

One typographic point, so 12pt equals 16px

1pc = 12pt

One pica, a sixth of an inch or 16px

rem

The root element's font size; browsers default to 16px unless the user changes it

Try it: css-unit-converterFrom the docs, not run by us
em

The element's own computed font size; on font-size itself it means the inherited size, so nested values multiply

Try it: css-unit-converterFrom the docs, not run by us
ch

Advance width of the 0 glyph; falls back to 0.5em when it can't be measured. Good for max-width: 65ch text columns

From the docs, not run by us
ex

The font's x-height, roughly 0.5em in many fonts; falls back to 0.5em

From the docs, not run by us
cap

Height of capital letters in the current font, handy for aligning icons to caps

From the docs, not run by us
lh

The element's computed line-height as a length, so margin-block: 1lh equals one line of text

From the docs, not run by us
rlh

Same as lh but read from the root element

From the docs, not run by us
ic

Advance of the CJK water ideograph, the ch equivalent for Chinese, Japanese and Korean text

From the docs, not run by us

MDN lists the same units at https://developer.mozilla.org/en-US/docs/Web/CSS/length and says vh equals lvh and vw equals lvw.

100vw / 100vh

1% of the viewport width or height each; the classic forms behave as the large viewport

From the docs, not run by us
100svh

Small viewport height: sized as if mobile browser bars are showing, so content always fits

From the docs, not run by us
100lvh

Large viewport height: sized as if the bars are hidden, so content can slip under them

From the docs, not run by us
100dvh

Dynamic viewport height: follows the bars as they show and hide, and may change while scrolling

From the docs, not run by us
vmin / vmax

1% of the smaller or larger of the viewport's two sides

From the docs, not run by us
vi / vb

1% of the viewport along the inline or block axis, which respects vertical writing modes

From the docs, not run by us

They measure the nearest ancestor with container-type set. With no such container they fall back to the small viewport units for that axis.

cqw / cqh

1% of the query container's width or height

From the docs, not run by us
cqi / cqb

1% of the container's inline or block size; cqi is the one to use for fluid text in components

From the docs, not run by us
cqmin / cqmax

1% of the smaller or larger of the container's inline and block sizes

From the docs, not run by us
clamp(1rem, 2.5vw, 2rem)

Fluid font size that tracks the viewport but never leaves the 1rem to 2rem range

From the docs, not run by us
clamp(MIN, VAL, MAX)

Resolves as max(MIN, min(VAL, MAX)); the units in each argument may differ

From the docs, not run by us
clamp(min(10vw, 20rem), 300px, max(90vw, 55rem))

Arguments can themselves be min() or max() expressions

From the docs, not run by us
font-size: max(1.2rem, 1.2vw)

Takes the larger value, giving text a floor that still grows on wide screens

From the docs, not run by us

Worked example: Fluid heading with an accessible ceiling

Say the design wants a heading at 28.8px on phones and 44.8px on a wide desktop. Convert to rem first so user font settings still apply: 28.8 / 16 = 1.8rem and 44.8 / 16 = 2.8rem.

h1 {
  font-size: clamp(1.8rem, 2.5vw, 2.8rem);
}

Now work out where each bound kicks in. 2.5vw is 2.5% of the viewport width. It reaches 28.8px at 28.8 / 0.025 = 1152px and 44.8px at 1792px. So below a 1152px window the heading sits at the 1.8rem floor, between 1152px and 1792px it grows smoothly, and above that it stops at 2.8rem.

That tells you the middle value is a bit lazy: most phones and laptops never see the fluid part. A preferred value like 1rem + 2vw moves the growth range down to ordinary laptop widths. MDN's accessibility advice is that the maximum should be at least twice the minimum when text sizes are clamped, so that 200% zoom still enlarges the text; with 1.8rem and 2.8rem it isn't, so for body text pick bounds like 1rem and 2rem.

Common mistakes

  • Using height: 100vh for a full-screen mobile hero

    vh is the large viewport, so the bottom hides behind the toolbar. Use 100svh, or 100dvh if it must track the bar.

  • Nesting em font sizes in lists and menus

    Each level multiplies the parent's size. Set font sizes in rem and keep em for padding and gaps that should follow the text.

  • Setting html { font-size: 62.5% } and then writing sizes in px anyway

    The trick only helps if you write rem everywhere. Pixel font sizes ignore the user's preferred size, so convert them to rem.

  • Using vw alone for font sizes

    Pure viewport text doesn't grow with browser zoom. Wrap it in clamp() with rem bounds so zoom and user settings still change the size.

  • Expecting cqi to work without declaring a container

    Make the parent a query container first; otherwise the unit quietly falls back to the small viewport.

Sources

How we research: where our facts come from, how often we re-check them, and why no one can pay for a review or a verdict.

What changed in AI dev tools, once a month

Renames, shutdowns, pricing changes and new MCP setups, each with a source.

Subscribe
Reviews and commentsShow

Comments

Sign in to leave a comment.

Loading comments…