CSS units: relative to what?
NOV 19, 2026 • 7 MIN. READ •
While building this blog with TailwindCSS, I noticed that almost everything is sized in rem:
text-base is 1rem, the spacing scale is based on 0.25rem, etc. I was used to writing px everywhere,
so I was like, wait a minute! Why rem and not px? And what about em, %, vh, vw...?
So I went back to the fundamentals and did some digging. It turns out that for CSS lengths, the unit tells you how to interpret the number:
2em, 50% or 10vw can each end up as 20px or 200px, depending on what they're measured against.
Some units are fixed, some follow the font size, some the containing block, some the screen and some the container.
So the key question for every unit is: relative to what? Once I knew the answer for each unit,
picking the right one got a lot easier.
I grouped the units into five types, based on what they're relative to:
- Absolute units:
px,pt,cm,mm,in - Font-relative units:
em,rem,ch - Percentages and grid fractions:
%,fr - Relative to the viewport:
vw,vh,vmin,vmax - Relative to a container:
cqw,cqh,cqi,cqb,cqmin,cqmax
Absolute units
Absolute units have a fixed size, they don't depend on anything else on the page.
px (pixel)
px is the one we all know. But a CSS px is not a physical screen pixel, it's a reference pixel.
On a retina screen with a device pixel ratio of 2, one CSS pixel spans 2 device pixels along each axis,
so a 1px × 1px square covers 2 × 2 = 4 device pixels (see window.devicePixelRatio).
.card {
border: 1px solid;
border-radius: 8px;
}The problem with px for text: if a user bumps the default font size in their browser settings
(e.g. from 16px to 20px), a font-size: 16px stays 16px. It ignores the user's preference.
pt (point), cm (centimeter), mm (millimeter), in (inch)
These are the "physical" units. In CSS they're all anchored to px:
1in= 1 inch =96px1cm= 1/2.54 of an inch =~37.8px1mm= 1/10 of a cm =~3.78px1pt= 1/72 of an inch =~1.33px
So 1cm on screen is not a real centimeter, it depends on the screen. They only really make sense
for print, where the browser maps them to real paper sizes:
@media print {
body {
font-size: 12pt;
margin: 2cm;
}
}On screen, we should not use them.
Font-relative units
em (the letter "M")
em isn't an abbreviation, it comes from typography, where it was the width of the capital letter M.
In CSS, em is relative to the font-size of the element itself. If the element has font-size: 20px, then 1em is 20px.
The one exception: when used in font-size, it's relative to the parent's font-size.
.button {
font-size: 1.25rem; /* 20px */
padding: 0.5em 1em; /* 10px 20px */
}That's handy: the padding scales with the text. Make the button text bigger and the padding follows.
The catch is that em compounds when nesting:
<ul style="font-size: 1.2em">
<li>
1.2x
<ul style="font-size: 1.2em">
<li>1.44x</li>
</ul>
</li>
</ul>Each level multiplies the one above it, which gets confusing fast.
rem (root em)
rem = root em. It's relative to the font-size of the root <html> element, which defaults to 16px in browsers.
The examples below assume that 16px root.
h1 {
font-size: 2rem; /* 32px */
}
p {
font-size: 1rem; /* 16px */
margin-bottom: 1.5rem; /* 24px */
}Unlike em, rem doesn't compound: 1rem is the same size everywhere on the page.
It also respects the user's choice. Browsers let users change their default font size in the settings.
If someone sets it to 20px, then 1rem becomes 20px, and everything sized in rem grows with it.
That's why TailwindCSS uses rem: it's the accessible default.
The catch: if you set html { font-size: 16px }, you lock it, and 1rem stays 16px whatever the user picked.
To change the root size, use a relative value like html { font-size: 112.5% } (18px by default) so the user's setting still applies.
ch (character)
ch is the width of the 0 character in the current font. It's perfect for limiting line length,
since text is easiest to read at around 60-75 characters per line:
article {
max-width: 65ch;
}Percentages and grid fractions
% (percentage)
% is relative to the containing block, which is usually the parent,
but not always. Usually, a position: absolute element uses its nearest positioned ancestor, and a position: fixed element uses the viewport.
The exception: an ancestor with transform, filter or contain (among others) becomes the containing block for both,
even without position. So a fixed element with width: 50% inside a transform: translateZ(0) wrapper is half of that wrapper, not of the screen.
And which size of the containing block depends on where you use it:
width: the containing block's widthheight: the containing block's height, but only if that height is definite (e.g. a fixedheight, or a stretched flex/grid item). Otherwise it behaves likeauto.padding/margin: the containing block's width, even for top and bottomfont-size: the parent'sfont-size(same asem)
.sidebar {
width: 25%;
}
.content {
width: 75%;
}The padding-top: % quirk used to be the go-to trick for responsive videos, nowadays aspect-ratio: 16 / 9 does the job.
fr (fraction)
fr only works in CSS grid. It's a fraction of the free space left in the grid container, after fixed sizes and gaps are taken out.
.layout {
display: grid;
grid-template-columns: 250px 1fr 2fr;
gap: 1rem;
}Here the first column is 250px, and the rest is split in 3: one part for the second column, two for the third.
Unlike %, you don't have to do the math for the gaps yourself.
One gotcha: 1fr is really minmax(auto, 1fr), so a column won't shrink below its content.
A long URL or unbreakable word can break the 1:2 split. Use minmax(0, 1fr) and minmax(0, 2fr) to force the proportions.
Relative to the viewport
vw (viewport width) and vh (viewport height)
1vw is 1% of the viewport width and 1vh is 1% of the viewport height.
.hero {
height: 100vh;
}Two gotchas I ran into:
100vwincludes the vertical scrollbar, sowidth: 100vwcan cause a horizontal scroll. Usewidth: 100%instead.- On mobile,
100vhis the height without the browser's address bar, so content gets hidden under it. The newer units fix this:svh(small, bars visible),lvh(large, bars hidden) anddvh(dynamic, updates as the bars move).
.hero {
height: 100dvh;
}vmin (viewport minimum) and vmax (viewport maximum)
1vmin is 1% of the smaller side of the viewport, 1vmax is 1% of the larger side.
Useful for things that should fit the screen in both portrait and landscape:
.logo {
width: 50vmin; /* always fits, whatever the orientation */
}Relative to a container
cqw, cqh, cqi, cqb, cqmin, cqmax
cq stands for container query:
cqw: container query widthcqh: container query heightcqi: container query inline sizecqb: container query block sizecqmin/cqmax: container query minimum / maximum
These work like the viewport units, but relative to a container instead of the screen.
1cqw is 1% of the container's width, 1cqh 1% of its height, and cqi/cqb follow the text direction (inline/block).
cqmin/cqmax are the smaller/larger of cqi and cqb.
Which container? For each axis, the nearest ancestor that can be queried on that axis:
container-type: inline-sizeonly works forcqi(andcqwin horizontal text)container-type: sizeworks for both axes
If there's no such ancestor, the unit falls back to the small viewport unit (cqi acts like svi, cqb like svb).
So cqi and cqb, and therefore cqmin/cqmax, can end up measuring different elements.
.card-wrapper {
container-type: inline-size;
}
.card-title {
font-size: clamp(1rem, 5cqi, 2rem);
}The title scales with the card's width, so the same card looks right in a narrow sidebar and in a wide main column.
So which one do I use?
remforfont-sizeand spacing: respects the user's font size settingemfor padding/margin that should scale with the component's own text (buttons, badges)pxfor things that should stay crisp and fixed: borders, shadows,1pxdividerschfor readable line length%for widths relative to the containing blockfrfor grid columns and rowsdvh/vw/vminfor full-screen sectionscq*for components that adapt to their containerpt,cm,mm,inonly in print stylesheets
So that's it, rem by default and the others when I actually need what they're relative to!
Further reading
- CSS values and units - MDN
<length>reference - MDN, every length unit in one page- Container size and style queries - MDN
- The large, small, and dynamic viewport units - web.dev
- The Surprising Truth About Pixels and Accessibility - Josh W. Comeau, on
pxvsrem - CSS Values and Units Module Level 4 - W3C spec