px, em, rem, %, vw, vh: Which CSS Unit When?
You probably won’t find this on Reddit, but the CSS1 Recommendation of 17 December 1996 defined the pixel as one dot at 90 dpi seen from 28 inches away (0.0227 degrees); today’s spec says 96 dpi and 0.0213 degrees.
Every CSS units thread ends with the same one-liner: use rem, not px.
I came to this page as the researcher: on December 1, 2026 I read the W3C CSS Values and Units draft, Media Queries Level 4, WCAG 2.2 and the original CSS1 Recommendation instead of the blogs that quote each other, then measured every unit in Chrome 158.
Three dates matter.
CSS1 of December 17, 1996 gave authors eight length units.
The Values and Units draft of March 12, 2024 defines 43.
And WCAG failure technique F94, updated March 9, 2026, names text sized in vw as a failure example, so I measured what zoom does to it.
A CSS unit is the word after the number, and it decides what a size follows: your font, the parent box, the window, or nothing at all.
If you like a printed reference beside the keyboard, the CSS Visual Dictionary paperback on Amazon draws properties and values as pictures.
Short answer: use rem for font sizes and most spacing, em when a size should follow its own text size, % for widths inside a parent, vw and dvh for sizes tied to the window, ch for line length, and px for hairlines. Each follows something different, which our Chrome 158 test measured.
What the diagram shows, in plain text (800px wide window, 16px root font, 20px Arial where a unit follows the text):
- Absolute units, fixed ratios to
px:1pt= 1.33px,1cm= 37.78px,1in= 96px. - Font-relative units, following the text:
1rem= 16px,1em= 20px,1ch= 11.11px,1ex= 10.36px. - Window-relative, following the viewport:
1vw= 8px. - Parent-relative, following the box around it:
1%of a 400px parent = 4px.
What does each CSS unit follow?
Every CSS length follows exactly one reference, and the unit tells you which. The W3C CSS Values and Units Module Level 4 groups them into absolute lengths, font-relative lengths and viewport-percentage lengths, and percentages follow the property’s own rule.
| Unit | What it follows | Measured in Chrome 158 |
|---|---|---|
px | the reference pixel, 1/96 of an inch | 1in = 96px |
em | the element’s own font size (the parent’s, when used on font-size) | 20px text: 1em = 20px |
rem | the font size of the root element | 16px root: 1rem = 16px |
% | whatever the property says (usually the parent’s width) | 50% of 400px = 200px |
vw, vh | 1% of the viewport’s width or height | 800px wide: 1vw = 8px |
ch | the width of the “0” glyph in the current font | 20px Arial: 1ch = 11.11px |
lh | the computed line height | 20px Arial, normal: 1lh = 23px |
pt, pc, cm | fixed multiples of the pixel | 1.33px, 16px, 37.78px |
In one line: three units follow text (em, rem, ch), two follow the window (vw, vh), one follows the parent (%), and the rest are fixed.
What is a CSS pixel, and is it a dot on my screen?
A CSS pixel (px) is not a screen dot. It is a unit defined as 1/96 of an inch, and the browser maps it onto however many device pixels your screen needs. Our Chrome 158 test measured 1in as exactly 96px.
The Values and Units draft says 1px = 1/96th of 1in
and defines the reference pixel by viewing distance: For a nominal arm’s length of 28 inches, the visual angle is therefore about 0.0213 degrees.
On a phone that packs several device dots into each CSS pixel, one px is still one reference pixel.
The idea is old. The CSS1 Recommendation of December 17, 1996 (Håkon Wium Lie and Bert Bos) already told browsers to rescale: Pixel units, as used in the last rule, are relative to the resolution of the canvas, i.e. most often a computer display.
It anchored the reference pixel at 90 dpi, not 96, which gives the 0.0227 degrees in the subtitle above.
The practical result: 1cm is 37.78px on a screen, but it will not measure 1cm with a ruler. MDN’s length reference says so itself: such dimensions don’t necessarily match the size of the physical unit with the same name
.
What is the difference between rem and em?
rem follows the root element’s font size and never changes, while em follows the font size of the element it is used on, so it compounds as you nest boxes. The spec’s wording is Equal to the computed value of the em unit on the root element
for rem.
We nested four boxes, each with font-size: 1.25em, inside a 16px root. The em chain grew every level, while the same chain written in rem stayed put.
| Nesting level | font-size: 1.25em | font-size: 1.25rem |
|---|---|---|
| 1 | 20px | 20px |
| 2 | 25px | 20px |
| 3 | 31.25px | 20px |
| 4 | 39.0625px | 20px |
That compounding is why a nested list in em gets bigger at every level. The arithmetic is 16 x 1.25 x 1.25 x 1.25 x 1.25 = 39.0625.
em has a second meaning that trips people up. On font-size it follows the parent; on every other property it follows the element’s own font size. MDN’s Values and units lesson states both halves in one sentence.
Say you give a button font-size: 1.5rem and padding: 0.5em 1em. The padding is 12px by 24px because it follows the button’s own 24px text, so one change to the font size resizes the whole button together. With rem padding it would stay fixed.
The same 1996 document explains why em values do not re-calculate in children. Its own example sets body { font-size: 12pt; text-indent: 3em } and h1 { font-size: 15pt }, then says Child elements inherit the computed value, not the relative value
: the h1’s indent is 36pt, not 45pt.
We ran that exact example in Chrome 158 on December 1, 2026. The h1 had a 20px font and a 48px indent (36pt), not the 60px (45pt) a recalculation would give.
Why does line-height: 1.5em squash the text in a bigger child?
An em line height is turned into a fixed length in the parent, and children inherit that length, not the rule. A unitless line-height: 1.5 is inherited as the number, so every child recalculates from its own font size.
We set a 16px parent and a 32px child. With line-height: 1.5em the computed value was 24px, and the child’s line was 24px tall, shorter than its own 32px text. With line-height: 1.5 the child’s line was 48px.
Should I use px or rem for font size?
Use rem for font sizes. Chrome’s default root size is 16px, so 1rem equals 16px for most visitors, but if a visitor raises their browser’s default text size, rem follows it and px does not.
The W3C’s Understanding Success Criterion 1.4.4 sets the bar: text can be resized without assistive technology up to 200 percent without loss of content or functionality
. Zooming handles that for both units. The default-size setting is where only rem responds.
| Written as | Default 16px | Default 20px | Default 24px |
|---|---|---|---|
font-size: 1rem | 16px | 20px | 24px |
font-size: 1.125rem | 18px | 22.5px | 27px |
font-size: 16px | 16px | 16px | 16px |
One popular trick is html { font-size: 62.5% }, so 1rem becomes 10px and 1.6rem is 16px. We confirmed both numbers in Chrome 158: the root measured 10px and a 1.6rem box computed to 16px.
It still follows the visitor’s setting, because a percentage of the default is relative. But you lose the clean 1rem = 16px, and every component then needs numbers like 1.6rem. Our pick: leave the root at 100% and read 0.75rem as 12px, 1rem as 16px and 1.5rem as 24px.
Keep px for things that should not scale with text: a 1px border, a hairline divider, a shadow offset.
What is a percentage in CSS a percentage of?
A percentage is a percentage of whatever the property’s own rule names, and the rule differs by property. Width is usually the parent’s width, but padding and margin take the parent’s width on all four sides, and font-size takes the parent’s font size.
The CSS Box Model Module Level 3 (Recommendation, 11 April 2024) says it plainly for both: Percentages: refer to logical width of containing block
. We measured one box with several percentages inside a 400px parent with 20px text.
| Declaration | What it followed | Measured |
|---|---|---|
width: 50% | parent’s width (400px) | 200px |
padding: 10% (left and top) | parent’s width (400px), even for the top | 40px and 40px |
margin-top: 10% | parent’s width (400px), not its height | 40px |
font-size: 150% | parent’s font size (20px) | 30px |
height: 50% in a parent with no set height | nothing: it behaved as auto | 80px (the content’s height) |
The last row answers “why is my height: 50% ignored”. A percentage height needs a parent with a definite height; give the parent a height, or use vh or dvh instead.
To mix a percentage with a fixed gap, use calc(): width: calc(50% - 20px) takes half the parent and leaves 20px for the gutter.
The same decision as text, row by row:
- Follows the user’s text-size setting:
rem, for font sizes, most spacing and page margins. - Follows this element’s own text size:
em, for button padding, icon size and space around a heading. - Follows the parent box:
%, for column widths and a child that fills its parent. - Follows the window:
vwanddvh, for a full-screen hero (keepvhas the fallback). - Follows a number of characters:
ch, for line length such asmax-width: 65ch. - Follows nothing:
px, for 1px borders, hairlines and shadows.
Why is 100vw wider than my page?
A 100vw box is as wide as the window including the scrollbar, while 100% is as wide as the space left after it. On a page with a classic scrollbar that is a 15px gap in Chrome, so width: 100vw causes a sideways scroll.
The spec says this outright: the viewport-percentage lengths are sized assuming that scrollbars do not exist
, with one exception: if the root’s overflow or scrollbar-gutter reserves space unconditionally, the values are reduced
. We tested all four cases on a 2,000px tall page with classic scrollbars in an 800px wide window.
| Root element setting | width: 100vw | width: 100% |
|---|---|---|
overflow: auto (the default) | 800px | 785px |
overflow: scroll | 785px | 785px |
scrollbar-gutter: stable | 785px | 785px |
overflow: hidden (no scrollbar) | 800px | 800px |
Our scrollbar was 15px wide. Chrome’s default overlay scrollbars took no width on our machine, so we switched classic scrollbars on to measure it. If you never see the sideways scroll yourself, your scrollbars may be overlay ones.
The fix for a full-width section is width: 100% on a block, which needs no width at all. Use vw for sizes that should follow the window, like a hero’s font-size or a square width: 50vw; height: 50vw.
What is the difference between vh, dvh, svh and lvh?
All four are 1% of the viewport height; they differ only when a phone’s address bar slides in and out. vh equals lvh, the large viewport (bar retracted); svh is the small one (bar showing); dvh follows the bar as it moves.
MDN says it in one line: vh is equivalent to lvh
. The spec warns about the dynamic one: The sizes of the dynamic viewport-percentage units are not stable even while the viewport itself is unchanged.
That means a dvh layout can resize while the visitor scrolls.
On our desktop window all four returned the same height, 513px, so desktop testing cannot show the difference. We did not test a phone; the split is the spec’s description, not our measurement.
Our pick for a full-screen hero: min-height: 100vh as the fallback, then min-height: 100svh so it never hides behind the address bar. Use dvh only where you accept a layout that moves.
.hero { min-height: 100vh; } /* older browsers */
.hero { min-height: 100svh; } /* never taller than the visible area */
What is the ch unit good for?
The ch unit is the width of the “0” glyph, so it counts characters. It is the best unit for line length: max-width: 65ch keeps a paragraph at roughly 65 characters per line in whatever font you pick.
Here’s a moment almost every newbie knows: you set a box to width: 100vw, it looks perfect, and a sideways scrollbar shows up anyway. It is not your layout. The window and the page are two different widths, and one of them includes the scrollbar.
The spec defines it as the used advance measure of the “0” (ZERO, U+0030) glyph in the font used to render it
. In our test, 20px Arial gave 1ch = 11.11px, so 65ch was 722px. Real letters vary in width, so the character count is approximate.
Its cousin ex is the x-height; our 20px Arial measured 10.36px. Of the two, ch is the one you will actually use.
Is it OK to size text in vw or clamp()?
Text sized only in vw does not grow when a visitor zooms, so use rem as part of the size. We measured four font sizes at 100% and at a simulated 200% browser zoom, and pure vw stayed at exactly 1.0 times.
Browser zoom shrinks the CSS viewport, so 200% zoom on a 1000px window means a 500px viewport. Real text size on screen is the CSS size times the zoom. We measured the CSS sizes in a 1000px and a 500px window and did that multiplication.
| font-size | At 100% | At 200% (CSS px x 2) | Growth |
|---|---|---|---|
2vw | 20px | 10 x 2 = 20px | 1.0 times |
1rem | 16px | 16 x 2 = 32px | 2.0 times |
calc(1rem + 1vw) | 26px | 21 x 2 = 42px | 1.62 times |
clamp(1rem, 2vw, 1.5rem) | 20px | 16 x 2 = 32px | 1.6 times |
The table is our arithmetic, not a WCAG conformance test. It shows the direction: any vw in the size eats part of the zoom.
The W3C’s failure technique F94 names the problem and sets the extreme case: .callout { font-size: 1vw; }. It says Attempts to use browser controls to zoom or adjust text-size will not work.
The same page softens it: Some uses of viewport units may not prevent text-size adjustments
.
Why does a media query in em ignore the font size I set on html?
Media queries do not read your CSS at all. Their em and rem are based on the browser’s initial font size, usually 16px, so (min-width: 40em) means 640px whatever you set on html.
Media Queries Level 4 (Candidate Recommendation Draft, 19 February 2026) says Relative length units in media queries are based on the initial value
, so units are never based on results of declarations
.
We set html { font-size: 32px } and asked matchMedia('(min-width: 40em)') in an 800px window. The answer was true, meaning the threshold was 640px, not the 1280px a 32px root would give.
The same held for 40rem. The upside: breakpoints in em grow with the visitor’s default text size, because that setting changes the initial value, and they ignore anything your own stylesheet does.
Where do the 1996 spec, MDN and WCAG disagree?
They disagree in four places, and one of them changes what “px” means. We list each source’s wording next to what Chrome 158 did.
| Question | One source | Another source | Chrome 158 |
|---|---|---|---|
| How big is the reference pixel? | CSS1 (1996): 90 dpi, visual angle about 0.0227 degrees at 28 inches | Values 4 (2024): 96 dpi, about 0.0213 degrees; 1px = 1/96in | 1in = 96px |
Is 1cm a real centimetre? | MDN lesson: absolute units are generally considered to always be the same size(1cm = 37.8px) | MDN length reference: they don’t necessarily match the size of the physical unit | 1cm = 37.78px; pixels, not millimetres on glass |
| Can text in viewport units be zoomed? | F94: zoom and text-size controls will not work | Same page: some uses may not preventadjustment | 2vw 1.0x; calc(1rem + 1vw) 1.62x; 1rem 2.0x |
What do vw and 100% include? | Spec: vw assumes no scrollbars | MDN length page: 1vw is 1% of the viewport width, no scrollbar mention | 100vw 800px vs 100% 785px |
The short version: the reference pixel moved from 90 dpi in 1996 to 96 dpi in the 2024 draft, so px is a ratio and not a measurement. The fourth row is the one the MDN pages we read leave out, though the spec states it.
Two more answers none of the pages we read gave in one place: em in a media query ignores your root font size (section above), and padding-top: 10% is 10% of the parent’s width (percent section).
Which CSS unit should I use for what?
Use rem for font sizes and spacing, em where a size should follow its own text, % or fr for widths, ch for line length, dvh or svh for window-tall sections, and px for hairlines. The table adds who each choice suits and where it fails.
| Job | Our pick | Who it suits | Where it fails |
|---|---|---|---|
| Body and heading font size | rem | every page; follows the visitor’s default size | none for text; avoid for a fixed icon |
| Button or badge padding | em | components whose size should track their own text | nested boxes compound the way our table showed |
| Page margins, section gaps | rem | layouts that should breathe with the text size | skip it for gaps tied to the window |
| Width inside a column | % with max-width | fluid cards and images | height percentages need a parent with a set height |
| Full-screen hero | svh, vh fallback | landing pages | dvh resizes while scrolling; not measured on a phone |
| Line length | ch | article text | glyph widths differ by font |
| 1px border, divider, shadow | px | anything that must stay thin | never use it for text you want to zoom with the default size |
| Breakpoints | em | sites that should react to the default text size | the number is 16px-based, not your root’s |
Skip pt, cm and in for screens: they are fixed multiples of the pixel with no screen advantage. They earn a place in print stylesheets, where the output size is known. CSS1 already said so: Absolute length units are only useful when the physical properties of the output medium are known.
Our guides on the box model and margin versus padding show where the spacing units land inside a box.
Do rem, dvh and container units work in every browser?
Yes, in every current browser. Can I use’s data file, updated November 27, 2026, shows each of these units around 96 to 97.6 percent of users, and MDN marks <length> as Baseline, available across browsers since July 2015
.
rem- 97.57% of users
vw,vh,vmin,vmax- 97.4% of users
ch- 97.4% of users
svh,lvh,dvh- 96.07% of users
- Container query units (
cqwand friends) - 96.03% of users
min(),max(),clamp()- 97.2% of users
Check Can I use before leaning on dvh for a launch that must reach the last four percent. The old vh fallback in the hero example above covers them.
Count what the language has grown to. CSS1 gave authors eight length units: em, ex, px, in, cm, mm, pt and pc.
We counted the 2024 Values draft’s definitions: 12 font-relative, 24 viewport and 7 absolute units, 43 in all, before container query units. Yet the decision above needs only six answers, because every unit answers one question: what should this size follow?
How we researched this
- Specifications read on December 1, 2026: W3C CSS Values and Units Module Level 4 (Working Draft, 12 March 2024), Media Queries Level 4 (Candidate Recommendation Draft, 19 February 2026), CSS Box Model Module Level 3 (Recommendation, 11 April 2024), and the original CSS1 Recommendation of 17 December 1996 (the unusual source: its pixel definition, its inheritance example and its list of eight length units).
- Accessibility and documentation read the same day: WCAG 2.2 Understanding Success Criterion 1.4.4 and failure technique F94, MDN’s length reference and Values and units lesson, and the Can I use data file (updated November 27, 2026).
- Searches: Google autocomplete for “css units”, “rem vs em”, “px vs rem”, “css vw” and “vh vs dvh”, plus the tf ac scan of 378 stems for “css units” (305 suggestions), on December 1, 2026. Headings follow those phrases.
- Our own tests: three small pages measured with getBoundingClientRect() and getComputedStyle() in headless Chrome 158 (macOS): twelve units in an 800px window; four nested em and rem boxes; line height in em and unitless; percentages in a 400px parent; 100vw against 100% with four root settings; a media query in em with a 32px root; the CSS1 text-indent example; font sizes in a 1000px and a 500px window. One browser, one run each.
- What we did not do: we did not test Firefox, Safari or a phone, so the dvh and svh split is the spec’s description, not our measurement; we did not change the real browser default font size (the table is arithmetic); and we did not run a WCAG conformance test. We counted no user threads.
Open a page you made, change one font-size from px to rem, then raise the default text size in your browser settings and watch which things move. Five minutes of that and each unit stops being a name to memorize and becomes a thing to follow.
Sources and further reading
- W3C: CSS Values and Units Module Level 4 — Working Draft, 12 March 2024: unit definitions, reference pixel, viewport units and scrollbars
- W3C: Cascading Style Sheets, level 1 — Recommendation, 17 December 1996: eight length units, 90 dpi reference pixel, inheritance example
- W3C: Media Queries Level 4 — Candidate Recommendation Draft, 19 February 2026: relative units use the initial value
- W3C: CSS Box Model Module Level 3 — Recommendation, 11 April 2024: padding and margin percentages
- W3C WAI: Understanding Success Criterion 1.4.4 Resize Text — the 200 percent rule
- W3C WAI: Failure F94, incorrect use of viewport units to resize text — updated 9 March 2026
- MDN Web Docs: <length> — Baseline since July 2015, vh equals lvh, absolute-unit anchoring
- MDN Web Docs: Values and units — em versus rem and the absolute units table
- Can I use: rem units — 97.57% of users, data file updated November 27, 2026
- Can I use: small, large and dynamic viewport units — 96.07% of users, data file updated November 27, 2026
Questions and answers
What is the difference between rem and em?
rem follows the root element’s font size and never compounds. em follows the font size of the element it is used on (the parent’s, when used on font-size), so nested boxes grow at every level. Use rem for font sizes and em for padding that should track its own text.
Should I use px or rem for font size?
Use rem. It follows the default text size a visitor sets in their browser, while px stays fixed. Keep px for 1px borders and hairlines.
Why is 100vw wider than my page?
100vw includes the scrollbar, and 100% does not. In Chrome with a classic 15px scrollbar, 100vw measured 800px against 785px for 100%, which causes a sideways scroll. Use width: 100% on blocks.
What is the difference between vh, dvh, svh and lvh?
All are 1% of the viewport height. vh equals lvh (address bar retracted), svh is the smaller viewport (bar showing) and dvh follows the bar as it moves. On a desktop all four are identical.
What is a percentage in CSS a percentage of?
Of whatever the property’s rule names. Width uses the parent’s width, font-size the parent’s font size, and padding and margin the parent’s width on all four sides. A percentage height needs a parent with a set height.