Learn CSS Visually - CSS explained one diagram at a time - flexbox, grid, the box model and more

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.

By · Updated

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.

Horizontal bar chart drawn to scale at three times size showing one of each CSS unit as measured in Chrome 158. 1pt is 1.33 pixels, 1 percent of a 400 pixel parent is 4 pixels, 1vw in an 800 pixel window is 8 pixels, 1ex in 20 pixel Arial is 10.36, 1ch is 11.11, 1rem with a 16 pixel root is 16, 1em in 20 pixel text is 20, 1cm is 37.78 and 1in is 96 pixels. Colours mark which units are absolute, follow the font size, follow the window or follow the parent.
One of each unit, measured with getBoundingClientRect() in Chrome 158 and drawn at three times its size.

What the diagram shows, in plain text (800px wide window, 16px root font, 20px Arial where a unit follows the text):

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.

Units, what they follow and what we measured — compiled by Learn CSS Visually from the W3C CSS Values and Units Module Level 4 (Working Draft, 12 March 2024) and our own Chrome 158 measurements, December 1, 2026
UnitWhat it followsMeasured in Chrome 158
pxthe reference pixel, 1/96 of an inch1in = 96px
emthe element’s own font size (the parent’s, when used on font-size)20px text: 1em = 20px
remthe font size of the root element16px root: 1rem = 16px
%whatever the property says (usually the parent’s width)50% of 400px = 200px
vw, vh1% of the viewport’s width or height800px wide: 1vw = 8px
chthe width of the “0” glyph in the current font20px Arial: 1ch = 11.11px
lhthe computed line height20px Arial, normal: 1lh = 23px
pt, pc, cmfixed multiples of the pixel1.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.

Four nested boxes, each font-size 1.25 of its reference — measured by Learn CSS Visually with getComputedStyle() in Chrome 158, December 1, 2026
Nesting levelfont-size: 1.25emfont-size: 1.25rem
120px20px
225px20px
331.25px20px
439.0625px20px

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.

What a font-size becomes when a visitor changes their browser’s default text size — arithmetic from the 16px default we measured in Chrome 158, December 1, 2026 (our calculation, not a user test)
Written asDefault 16pxDefault 20pxDefault 24px
font-size: 1rem16px20px24px
font-size: 1.125rem18px22.5px27px
font-size: 16px16px16px16px

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.

One box inside a 400px wide parent, measured with getComputedStyle() and getBoundingClientRect() in Chrome 158, December 1, 2026
DeclarationWhat it followedMeasured
width: 50%parent’s width (400px)200px
padding: 10% (left and top)parent’s width (400px), even for the top40px and 40px
margin-top: 10%parent’s width (400px), not its height40px
font-size: 150%parent’s font size (20px)30px
height: 50% in a parent with no set heightnothing: it behaved as auto80px (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.

Six rows, each pairing a question about what a size should follow with the CSS unit that answers it. The user's text size setting points to rem for font sizes and most spacing. An element's own text size points to em for button padding. The parent box points to percent for column widths. The window points to vw and dvh for a full-screen hero. The number of characters points to ch for line length. Nothing that scales points to px for hairlines and borders.
Ask what a size should follow, then read across to the unit.

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: vw and dvh, for a full-screen hero (keep vh as the fallback).
  • Follows a number of characters: ch, for line length such as max-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.

Width of a 100vw box and a 100% box with classic scrollbars — measured by Learn CSS Visually in Chrome 158 (800px window), checked against the W3C CSS Values and Units Module Level 4, December 1, 2026
Root element settingwidth: 100vwwidth: 100%
overflow: auto (the default)800px785px
overflow: scroll785px785px
scrollbar-gutter: stable785px785px
overflow: hidden (no scrollbar)800px800px

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.

Text size growth at 200% browser zoom — computed by Learn CSS Visually from Chrome 158 font sizes measured in a 1000px window (zoom 100%) and a 500px window (the CSS viewport at zoom 200%), December 1, 2026
font-sizeAt 100%At 200% (CSS px x 2)Growth
2vw20px10 x 2 = 20px1.0 times
1rem16px16 x 2 = 32px2.0 times
calc(1rem + 1vw)26px21 x 2 = 42px1.62 times
clamp(1rem, 2vw, 1.5rem)20px16 x 2 = 32px1.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.

Where the sources differ — compiled by Learn CSS Visually from CSS1 (17 December 1996), the W3C CSS Values and Units Module Level 4 (Working Draft, 12 March 2024), MDN’s length reference and Values and units lesson, and WCAG 2.2 failure F94 (updated 9 March 2026), plus our Chrome 158 tests, December 1, 2026
QuestionOne sourceAnother sourceChrome 158
How big is the reference pixel?CSS1 (1996): 90 dpi, visual angle about 0.0227 degrees at 28 inchesValues 4 (2024): 96 dpi, about 0.0213 degrees; 1px = 1/96in1in = 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 unit1cm = 37.78px; pixels, not millimetres on glass
Can text in viewport units be zoomed?F94: zoom and text-size controls will not workSame page: some uses may not prevent adjustment2vw 1.0x; calc(1rem + 1vw) 1.62x; 1rem 2.0x
What do vw and 100% include?Spec: vw assumes no scrollbarsMDN length page: 1vw is 1% of the viewport width, no scrollbar mention100vw 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.

Which unit for what — compiled by Learn CSS Visually from the W3C CSS Values and Units Module Level 4 and our Chrome 158 measurements, December 1, 2026
JobOur pickWho it suitsWhere it fails
Body and heading font sizeremevery page; follows the visitor’s default sizenone for text; avoid for a fixed icon
Button or badge paddingemcomponents whose size should track their own textnested boxes compound the way our table showed
Page margins, section gapsremlayouts that should breathe with the text sizeskip it for gaps tied to the window
Width inside a column% with max-widthfluid cards and imagesheight percentages need a parent with a set height
Full-screen herosvh, vh fallbacklanding pagesdvh resizes while scrolling; not measured on a phone
Line lengthcharticle textglyph widths differ by font
1px border, divider, shadowpxanything that must stay thinnever use it for text you want to zoom with the default size
Breakpointsemsites that should react to the default text sizethe 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 (cqw and 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

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.

How this was written: Written from the 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), the original CSS1 Recommendation of 17 December 1996, WCAG 2.2 (SC 1.4.4 and failure technique F94), MDN Web Docs’ length reference and Values and units lesson, Can I use, and Google autocomplete for "css units", "rem vs em", "px vs rem", "css vw" and "vh vs dvh", all read on December 1, 2026. Every pixel value in the diagrams and tables is a measurement we took in Chrome 158 on that date, or arithmetic from those numbers. Code samples are simplified for learning.

Oh, one more thing…

If this helped, you’ll like our free updates: the insights we discover while researching, and the moments worth sharing with other people learning CSS — no spam, no promotions.

One short email when there’s something worth your time. Unsubscribe any time.