CSS Units Explained: px, rem, em, vh and ch
Absolute and relative units each have a place. Learn when to use px, rem, em, vh, vw, ch and the newer viewport units in CSS.
By Velloreth
Absolute units
.box { width: 200px; }
px is the only absolute unit you need. It is a CSS pixel, which is not a physical pixel on a high-density display.
Use px for a border, a shadow, and a small fixed detail. Do not use it for a font size, because it ignores the user’s browser setting.
The other absolute units (cm, mm, in, pt, pc) are for print and are rarely used on screen.
Font-relative units
.text { font-size: 1.25rem; }
rem is relative to the root font size, which is 16px by default. It scales with the user’s browser setting, which is why it is the right choice for text.
em is relative to the parent element’s font size, so it compounds when nested.
.parent { font-size: 1.5rem; } /* 24px */
.child { font-size: 1.5em; } /* 36px, not 24px */
The compounding is the trap. em is useful for a component that should scale with its own font size, such as a button’s padding.
.button {
font-size: 1rem;
padding: 0.5em 1em; /* scales with the button's font size */
}
ex is the height of a lowercase x, and it is rarely used.
ch is the width of the zero character.
.prose { max-width: 65ch; }
This is a good default for a readable line length, and it adapts to the font.
Viewport units
.hero { height: 100vh; }
vh is 1% of the viewport height, and vw is 1% of the width.
The mobile problem: 100vh on a mobile browser includes the area behind the address bar, so the element is taller than the visible area and the page jumps as the bar appears.
The newer units fix it:
dvh— the dynamic viewport height, which changes as the chrome appears and disappears.svh— the small viewport height, with the chrome visible.lvh— the large viewport height, with the chrome hidden.
.hero { min-height: 100dvh; }
Use dvh for a full-height section, and svh when you want the element to fit without a jump.
vw has a scrollbar problem: 100vw includes the scrollbar width, which causes a horizontal overflow. Use 100% instead.
Percentages
A percentage is relative to a different thing depending on the property.
| Property | Relative to |
|---|---|
width | The parent’s width |
height | The parent’s height |
font-size | The parent’s font size |
line-height | The element’s own font size |
padding | The parent’s width, even for vertical padding |
The padding case is the surprising one. A vertical padding of 10% is 10% of the parent’s width, not its height.
Fluid sizing with clamp
h1 {
font-size: clamp(1.5rem, 4vw + 1rem, 3rem);
}
clamp(min, preferred, max) returns the preferred value, bounded by the minimum and the maximum.
This is how a fluid font size is written, and it removes the need for a media query for a simple scale.
The preferred value is usually a viewport unit plus a base, so the size grows with the screen but not linearly.
Choosing a unit
The choice of unit is really a question about what the value should be relative to. A value that should scale with the user’s text preference is a rem, which is why a font size and a spacing derived from it belong in rem. A value that should scale with a component’s own font size is an em, which is why a button’s padding is often written in em so it grows with the label. A value that should scale with the viewport is a viewport unit, which is why a full-height hero uses dvh and a fluid heading uses a viewport unit inside clamp. A value that should never change is a px, which is why a border and a shadow are written in px. The mistake that causes the most trouble is using px for text, because it ignores the browser’s font size setting and makes the page harder to read for a user who has increased it. The second most common mistake is using a viewport unit for text without a rem base, which produces a size that is too small on a narrow screen and too large on a wide one.
Units and accessibility
The choice of unit has a direct effect on accessibility, and it is worth understanding why rather than following a rule. A user who has difficulty reading small text can increase the default font size in their browser, and a page that uses rem for its text respects that setting while a page that uses px ignores it. The same user may zoom the page instead, which scales everything including a px value, but a zoom is a worse experience because it also magnifies the layout and forces horizontal scrolling. A viewport unit is the third case: it scales with the screen rather than with the user’s preference, so a heading written purely in vw becomes smaller on a narrow screen regardless of what the user has chosen. The practical rule that follows is to use rem for anything that should respond to the user’s text preference, to use px only for a detail that should not scale, and to include a rem in any fluid value so the user’s setting still has an effect.
Practical guidance
- Use
remfor a font size. - Use
pxfor a border and a small detail. - Use
emfor a component that scales with its own font size. - Use
chfor a line length. - Use
dvhrather thanvhfor a full-height section. - Use
100%rather than100vwto avoid a scrollbar overflow. - Use
clamp()for a fluid size. - Remember a percentage is relative to a different thing per property.
For the related guides, see fluid-typography-with-clamp and responsive-design-breakpoints. For the broader context, css-box-model-explained and css-custom-properties.
Quotable summary
px is absolute and does not scale with the user’s font size, which is an accessibility concern for text, so use rem for a font size because it scales with the user’s preference. em is relative to the parent font size and compounds when nested, which makes it useful for a component that should scale with its own font size, such as a button’s padding. vh and vw are relative to the viewport, and the newer dvh and svh handle the mobile browser chrome that makes 100vh jump. ch is the width of a zero character, which makes it useful for a readable line length such as max-width: 65ch, and clamp() returns a preferred value bounded by a minimum and a maximum, which is how a fluid font size is written.