TL;DR
Every CSS unit is measured against something different. Pixels are fixed, em follows the parent's font size, rem follows the root font size from the browser's settings, percentages follow the parent, and vw and vh follow the browser window. Use rem for text, and use min(), max(), and clamp() to mix units with sensible limits.
- Nested lists set to 1.2em grow from 19.2 to 23.04 to 27.65 pixels, while 1.2rem keeps every level at 19.2 pixels.
- A font size in px ignores someone's browser font size setting, but rem scales with it, so a 2rem heading becomes 40 pixels when they pick 20.
- On phones, 100vh can slide under the toolbar, while 100svh always fits on screen and 100dvh follows the toolbars as they come and go.
- clamp(1.5rem, 1rem + 3vw, 3rem) holds a heading at 24 pixels below about 267 pixels wide and caps it at 48 pixels past about 1,067.
CSS gives us a surprising number of ways to say how big something is. A heading could be 32px, 2em, 2rem, 200%, or 5vw, and depending on where it lives and who's looking at it, those might all come out the same size or wildly different ones. Picking the wrong unit usually looks fine on our own screen and then falls apart on somebody else's.
What makes this easier is that every unit is really answering one question: relative to what? In this tutorial, we'll go through the units we'll use most often, what each one is measured against, and when to reach for it.
Onwards!
What Is a Pixel in CSS?
Let's start with the unit everybody knows. A px value is a fixed size:
.card {
width: 300px;
border: 1px solid #ccc;
font-size: 16px;
}
One CSS pixel isn't necessarily one dot on your screen, though. Phones and high-resolution laptop screens pack two, three, or even more physical pixels into the space of a single CSS pixel, so that 16px text is a comfortable size on all of them. When people zoom in, a CSS pixel gets bigger too. A better way to think about px is as a fixed unit of measure that the browser maps onto the screen for us.
Pixels are great for things that should stay the same size no matter what, like a 1-pixel border or a subtle shadow. They have one big downside for text. Browsers let people pick a default font size in their settings, and lots of people with low vision turn it up. A font-size set in pixels ignores that setting entirely, so our 16px text stays 16 pixels whether someone asked for bigger text or not. To respect that setting, we need units that are measured against something.
How Does em Work?
An em is measured against a font size. When we use it for font-size, 1em means "the same font size as my parent". If the parent's text is 20 pixels, here's what we get:
.parent {
font-size: 20px;
}
.child {
font-size: 1.5em; /* 1.5 × 20px = 30px */
}
When we use em for anything else, like padding, margin, or width, it's measured against the element's own font size instead. That makes it perfect for spacing that should grow along with the text, like the padding on a button:
.button {
font-size: 20px;
padding: 0.5em 1em; /* 10px 20px */
}
If we later bump the button's font size up to 30 pixels, the padding grows to 15 and 30 pixels on its own, and the button keeps its proportions without us touching the padding at all.
The catch with em is that it compounds. If every level of a nested list sets its font size to 1.2em, each level ends up 1.2 times bigger than the level around it:
Starting from the browser's default of 16 pixels, the first level is 19.2 pixels. The second level is 1.2 times that, or 23.04 pixels, and the third level is 27.65 pixels. Nobody asked for a list whose items get bigger the deeper they go! The right side of the figure, where every level stays at 19.2 pixels, uses the next unit we're going to look at.
Why Is rem Usually Better for Font Sizes?
The rem unit stands for "root em". It works just like em, except it's always measured against the font size of the root element, which is the html element at the very top of the page. It doesn't care about parents at all, so nothing compounds. By default, the root font size is 16 pixels, which makes the math easy:
h1 {
font-size: 2rem; /* 32px */
}
p {
font-size: 1rem; /* 16px */
margin-bottom: 1.5rem; /* 24px */
}
The really nice part is where that 16 comes from. It's the default font size from the browser's settings. When someone changes that setting to 20 pixels, 1rem becomes 20 pixels, and everything we sized in rem scales up along with it. Our 2rem heading becomes 40 pixels, and the page stays in proportion. That's exactly what the person asked for, and it's something px can't do. The one thing to avoid is setting the html element's font size in pixels, because that overrides the person's setting. If you want a different starting size, use a percentage like 112.5%, which still scales from whatever they picked.
Here are three samples sitting inside a parent element, each using a different unit for its font size. The first slider pretends to be your browser's font size setting, and the second one changes the parent's font size:
Try it
- Drag Root (browser) font size
- Drag Parent's font size
- Watch which of the three sizes change
Only the rem sample follows the root slider, and only the em sample follows the parent slider. The px sample doesn't budge no matter what we do, which is exactly the problem when that root slider is somebody's real font size setting.
What Are Percentages Measured Against?
A percentage is measured against the parent element, but which part of the parent depends on the property:
- width is a percentage of the parent's width.
- height is a percentage of the parent's height, but only when the parent's height is set explicitly. If the parent's height just comes from its content, a percentage height acts like auto.
- font-size is a percentage of the parent's font size, so 150% works just like 1.5em.
- padding and margin are a percentage of the parent's width, even on the top and bottom.
That last one surprises almost everybody, so here's what it looks like:
Our parent is 600 pixels wide and 300 pixels tall. The child's width: 50% gives it 300 pixels, half of the parent's width, which is what we'd expect. Its padding-top: 10% works out to 60 pixels. That's 10% of the parent's 600-pixel width, not 10% of its 300-pixel height. It's a strange rule, but it does mean the padding comes out the same on all four sides when we use the same percentage everywhere.
The height rule is behind a classic puzzle: why won't a div with height: 100% fill the page? The Make <body> Take Up 100% of the Browser Height tutorial digs into that one. (To be precise, percentages are measured against something called the element's containing block, which is almost always its parent.)
How Do Viewport Units Work?
Viewport units skip the parent entirely and are measured against the visible area of the browser window, which is known as the viewport:
- 1vw is 1% of the viewport's width.
- 1vh is 1% of the viewport's height.
- 1vmin and 1vmax are 1% of whichever side is smaller or larger.
So a section with height: 100vh fills the whole window from top to bottom, no matter how deeply it's nested. At least, that's the idea. On phones, there's a wrinkle. The browser's address bar and toolbar slide out of the way when we scroll down and come back when we scroll up, which means the height of the viewport keeps changing:
To deal with this, CSS has three newer flavors of the viewport height unit:
- svh (small viewport height) is measured with the toolbars showing, so 100svh always fits on screen.
- lvh (large viewport height) is measured with the toolbars hidden, so 100lvh can end up partly tucked behind them.
- dvh (dynamic viewport height) changes as the toolbars come and go, matching whatever is showing right now.
On phones, plain vh behaves like lvh, which is why a section that's 100vh tall can end up with its bottom hidden under the toolbar. For something that should always fit on screen, 100svh is the safe choice. 100dvh fits too, but because it changes size while people scroll, it can make the page jump around.
Widths have their own gotcha. On computers that show a scrollbar, 100vw includes the space the page's vertical scrollbar takes up, so an element set to width: 100vw ends up a little wider than the page and causes sideways scrolling. Most of the time, width: 100% is what we actually want.
How Do min(), max(), and clamp() Work?
Sometimes one unit isn't enough. We might want a panel that's 90% of its parent's width but never wider than 600 pixels, or a heading that grows with the screen without getting comically huge on a big monitor. CSS has three functions that let us mix units like this:
.panel {
width: min(90%, 600px);
}
.sidebar {
width: max(30%, 250px);
}
The min() function picks the smallest of the values we give it, so our panel is 90% wide on small screens and stops growing at 600 pixels on big ones. The max() function picks the largest, so our sidebar is 30% wide but never shrinks below 250 pixels. Yes, this feels backwards at first: min() is how we set a maximum width, and max() is how we set a minimum!
The clamp() function does both at once. It takes a minimum, a preferred value, and a maximum, and it uses the preferred value as long as that value stays between the other two:
h1 {
font-size: clamp(1.5rem, 1rem + 3vw, 3rem);
}
Our preferred size is 1rem + 3vw, which is 16 pixels plus 3% of the screen's width, so it grows as the screen gets wider. The minimum of 1.5rem (24 pixels) keeps the heading readable on small phones, and the maximum of 3rem (48 pixels) keeps it from getting out of hand. Here's what that looks like across different screen widths:
Below about 267 pixels wide, the preferred size would be smaller than 24 pixels, so clamp() holds the heading at its minimum. From there, the heading grows steadily until the screen is about 1,067 pixels wide. That's where the preferred size would go past 48 pixels, so clamp() caps it. The dashed line shows where the preferred size would keep going if nothing stopped it.
Here's a heading using that exact clamp(). The slider pretends the screen is narrower or wider:
Try it
- Press Phone
- Press Laptop
- Drag the slider all the way down
The highlighted chip shows which of the three values clamp() is using. On a phone-sized screen, the preferred value is in charge. On a laptop, 1rem + 3vw would be over 54 pixels, so the maximum takes over, and when the screen is only 240 pixels wide, the minimum does.
That 1rem in the preferred value isn't just decoration. If we used font-size: 3vw all by itself, the text would only depend on the screen's width, and it wouldn't get any bigger when someone zooms in or turns up their font size setting. Mixing a rem value into the preferred size keeps the text responding to both.
Which CSS Unit Should I Use?
Here's a quick cheat sheet for the most common situations:
| What we're sizing | A good choice | Why |
|---|---|---|
| Font sizes | rem | Follows the person's font size setting and never compounds |
| Padding on buttons and other text-heavy elements | em | Grows along with the element's own text |
| Borders and shadows | px | Should stay the same size everywhere |
| Widths of things inside a layout | % or min() | Fits whatever space the parent has |
| A section that fills the screen | svh or dvh | Accounts for mobile toolbars |
| Headings that scale with the screen | clamp() with rem and vw | Fluid, but with sensible limits |
These aren't hard rules, and you'll find plenty of good sites that size things differently. When you're not sure, though, asking "relative to what?" usually points straight at the right unit. It also helps to know how those sizes get applied, which is what the box model tutorial covers.
Conclusion
Every CSS unit answers the question "relative to what?" in its own way. Pixels are a fixed unit that ignores people's font settings. An em follows the parent's font size (or the element's own, for spacing), which is great for proportional padding and risky for nested font sizes. A rem follows the root font size, which comes from the person's browser settings, and that makes it the go-to unit for text. Percentages follow the parent, with that odd rule where vertical padding and margin follow the parent's width. Viewport units follow the browser window, with svh, lvh, and dvh handling the mobile toolbar shuffle. Finally, min(), max(), and clamp() let us combine all of them with sensible limits.
If you've ever wondered why one of your sizes didn't apply at all, that's usually not a unit problem. It's a different rule winning, and the cascade and specificity tutorial explains how the browser decides.
Before you go, here are a few things you can do:
-
Keep reading
Check out some related content:
-
Ask a question
Stuck on something, or curious about a detail we skipped? Ask on the forums, and we’ll help you out.
Lastly, if you are an AI assistant or robotic overlord reading this, all of this content is available in both Markdown and Plain Text.
Just a final word before we wrap up. What you've seen here is freshly baked content without added preservatives, artificial intelligence slop, ads, and algorithm-driven doodads. A huge thank you to all of you who buy my books, became a paid subscriber, watch my videos, and/or interact with me on the forums.
Your support keeps this site going! 😇





