CSS units
Overview
CSS has dozens of units, but you’ll use a handful most of the time. Choosing the right one makes layouts adapt naturally — to the screen, to the content and to the user’s own settings.
Absolute: px
A CSS pixel (px) is a fixed unit — not necessarily one physical pixel (a phone might use three device pixels per CSS pixel), but always the same size on a given screen. Use it for things that shouldn’t scale with text: hairline borders, shadows, small fixed details.
border: 1px solid #d6d6d2;
box-shadow: 0 2px 8px rgb(0 0 0 / 0.1);Other absolute units (cm, mm, in, pt) exist mainly for print stylesheets.
Font-relative: rem and em
rem (“root em”) is relative to the font size of the root <html> element — 16px by default. Crucially, the default comes from the user’s browser settings. Someone with low vision who sets their default font to 20px gets 1rem = 20px, and every rem-based size on your page grows with it.
h1 { font-size: 2.5rem; } /* 40px by default, 50px for that user */
.section { padding-block: 4rem; }
.sidebar { width: 18rem; }Sizes in px ignore that setting, so text in px doesn’t respect the user’s choice (browser zoom still works, but not the font-size preference). Use rem for font sizes, spacing and most layout sizes.
Don’t change the root font size to make maths easier (html { font-size: 62.5%; }) — it fights the user’s setting and complicates every component.
em is relative to the element’s own font size (or, for font-size itself, the parent’s). It’s ideal for things that should scale with the text they belong to:
.button {
font-size: 1rem;
padding: 0.75em 1.25em; /* 12px × 20px; bigger buttons get bigger padding automatically */
border-radius: 0.5em;
}
.button.large { font-size: 1.25rem; } /* padding scales too */Be careful with nested em font sizes — they compound: font-size: 1.2em inside another 1.2em is 1.44× the base. Using rem for font sizes avoids that.
Percentages
A percentage is relative to something in the containing block, usually its width:
.column { width: 50%; } /* half the container's width */
.inset { padding: 5%; } /* 5% of the container's WIDTH, even top/bottom */
.child { height: 50%; } /* only works if the parent has a definite height */Viewport units
Viewport units are relative to the browser window:
| Unit | 1 unit equals |
|---|---|
vw / vh |
1% of the viewport width / height |
vmin / vmax |
1% of the smaller / larger dimension |
svh, lvh, dvh |
1% of the small, large or dynamic viewport height |
vi / vb |
1% of the viewport in the inline / block direction |
The mobile height problem
On phones, browser toolbars appear and disappear as you scroll, so the viewport height changes. Classic vh uses the largest possible height, so height: 100vh sections are partly hidden behind the toolbars when the page loads.
The newer units fix this:
svh— the small viewport: toolbars visible. Content never hides behind them.lvh— the large viewport: toolbars hidden (whatvhusually means).dvh— the dynamic viewport: updates as the toolbars show and hide.
.hero { min-height: 100dvh; }dvh fits the screen exactly at all times, but it can make content resize while scrolling; svh is a stable alternative. These units are supported in all modern browsers.
A related trap: 100vw includes the vertical scrollbar on desktop, so width: 100vw can cause a horizontal scrollbar. Use width: 100% for full-width blocks instead.
Content-relative: ch and ex
ch is the width of the “0” glyph in the current font. It’s perfect for limiting line length:
.prose { max-width: 65ch; } /* comfortable reading width */
input.postcode { width: 8ch; } /* fits about 8 characters */Grid: fr
fr is a fraction of the free space in a grid container — covered fully in Tracks and the fr unit.
Combining units
calc() mixes units, and min(), max() and clamp() choose between them:
.main { width: calc(100% - 18rem); }
.card { width: min(100%, 24rem); }
h1 { font-size: clamp(2rem, 1.25rem + 3vw, 3.5rem); }Including a rem part in fluid font sizes (like 1.25rem + 3vw) keeps text responsive to zoom and font settings — pure vw text doesn’t grow when users zoom.
Quick guide
| For… | Use |
|---|---|
| font sizes | rem (or clamp() with rem) |
| spacing and most sizes | rem |
| padding inside buttons and badges | em |
| fluid widths | %, fr, or nothing at all (auto) |
| line lengths | ch |
| full-screen sections | dvh / svh with min-height |
| borders, shadows | px |
Respect the user: if you only remember one thing from this lesson, size text in
rem, notpx.
A full-height mobile hero
Build a .hero section that fills the visible screen on phones without being hidden behind the browser toolbars (use the right viewport unit), has padding of 1.5rem, and a heading whose size is set in rem. Add a .button whose horizontal padding is 1.25em and vertical padding is 0.75em, so it scales if you change its font-size. Then test: change the browser’s default font size (Settings → Appearance → Font size) and check that the heading and button grow.
<section class="hero">
<h1>Learn CSS layout</h1>
<a class="button" href="#start">Start now</a>
</section>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
/* your code here */
</style><section class="hero">
<h1>Learn CSS layout</h1>
<a class="button" href="#start">Start now</a>
</section>
<style>
* { box-sizing: border-box; }
body { margin: 0; }
.hero {
min-block-size: 100dvh; /* the screen as it is right now */
padding: 1.5rem;
display: grid;
align-content: center;
gap: 1rem;
}
.hero h1 {
font-size: 2.5rem; /* scales with the user's font-size setting */
margin: 0;
}
.button {
justify-self: start;
font-size: 1.125rem;
padding: 0.75em 1.25em; /* relative to the button's own font size */
background: #2d5bd7;
color: white;
border-radius: 0.5em;
text-decoration: none;
}
</style>Summary
pxis an absolute CSS pixel;remis relative to the root font size and respects user settings.emis relative to the element’s own font size — good for padding that scales with text.- Percentages are relative to the containing block, usually its width.
vw/vhare 1% of the viewport; on mobile preferdvh,svhorlvhovervh.- Use
chfor line lengths,frfor grid tracks, and combine units withcalc(),min(),max()andclamp().