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.
1pxOne CSS pixel, defined as 1/96 of an inch; a high-density screen paints it with several device pixels
1in = 96pxOne inch, and the anchor every other absolute unit is derived from
1cm = 96px / 2.54One centimetre, about 37.8px; useful in print stylesheets
1mm = 1cm / 10One millimetre, about 3.78px
1Q = 1cm / 40A quarter millimetre, about 0.94px
1pt = 1in / 72One typographic point, so 12pt equals 16px
1pc = 12ptOne pica, a sixth of an inch or 16px
remThe root element's font size; browsers default to 16px unless the user changes it
emThe element's own computed font size; on font-size itself it means the inherited size, so nested values multiply
chAdvance width of the 0 glyph; falls back to 0.5em when it can't be measured. Good for max-width: 65ch text columns
exThe font's x-height, roughly 0.5em in many fonts; falls back to 0.5em
capHeight of capital letters in the current font, handy for aligning icons to caps
lhThe element's computed line-height as a length, so margin-block: 1lh equals one line of text
rlhSame as lh but read from the root element
icAdvance of the CJK water ideograph, the ch equivalent for Chinese, Japanese and Korean text
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 / 100vh1% of the viewport width or height each; the classic forms behave as the large viewport
100svhSmall viewport height: sized as if mobile browser bars are showing, so content always fits
100lvhLarge viewport height: sized as if the bars are hidden, so content can slip under them
100dvhDynamic viewport height: follows the bars as they show and hide, and may change while scrolling
vmin / vmax1% of the smaller or larger of the viewport's two sides
vi / vb1% of the viewport along the inline or block axis, which respects vertical writing modes
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 / cqh1% of the query container's width or height
cqi / cqb1% of the container's inline or block size; cqi is the one to use for fluid text in components
cqmin / cqmax1% of the smaller or larger of the container's inline and block sizes
clamp(1rem, 2.5vw, 2rem)Fluid font size that tracks the viewport but never leaves the 1rem to 2rem range
clamp(MIN, VAL, MAX)Resolves as max(MIN, min(VAL, MAX)); the units in each argument may differ
clamp(min(10vw, 20rem), 300px, max(90vw, 55rem))Arguments can themselves be min() or max() expressions
font-size: max(1.2rem, 1.2vw)Takes the larger value, giving text a floor that still grows on wide screens
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: 100vhfor a full-screen mobile herovhis the large viewport, so the bottom hides behind the toolbar. Use100svh, or100dvhif it must track the bar.Nesting
emfont sizes in lists and menusEach level multiplies the parent's size. Set font sizes in
remand keepemfor padding and gaps that should follow the text.Setting
html { font-size: 62.5% }and then writing sizes in px anywayThe trick only helps if you write rem everywhere. Pixel font sizes ignore the user's preferred size, so convert them to rem.
Using
vwalone for font sizesPure 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
cqito work without declaring a containerMake the parent a query container first; otherwise the unit quietly falls back to the small viewport.
Related cheat sheets
Sources
- https://developer.mozilla.org/en-US/docs/Web/CSS/length
- https://www.w3.org/TR/css-values-4/
- https://developer.mozilla.org/en-US/docs/Web/CSS/clamp
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.