CSS Units
Almost every size in CSS needs a unit: font sizes, widths, padding, margins and gaps. Picking the right unit is the difference between a layout that adapts gracefully to any screen and one that breaks on a small phone.
Absolute units: px
The pixel (px) is the main absolute unit. A CSS pixel is not necessarily one dot on the screen: on high-resolution phones one CSS pixel may cover two or three physical pixels, so things look the same size on different devices. Pixels are predictable and good for small details such as borders (1px solid) and shadows. Other absolute units (cm, mm, in, pt) are mostly used for print stylesheets.
Relative units
Percentages (%)
A percentage is relative to something else, usually the parent. width: 50% makes an element half as wide as its containing block. For font-size, a percentage is relative to the parent's font size.
em
For font-size, 1em equals the parent's font size. For other properties such as padding, 1em equals the element's own font size. That makes em great for padding that should grow with the text, such as button padding.
The catch is that em font sizes compound when elements are nested:
<div style="font-size: 1.5em"> Outer text <div style="font-size: 1.5em">Inner text</div> </div>
With the default root size of 16px, the outer text is 16 × 1.5 = 24px, and the inner text is 24 × 1.5 = 36px. Nest a list inside a list styled with em and the text keeps growing.
rem
rem means "root em": it is always relative to the font size of the root <html> element, which is 16px by default in every major browser. 2rem is 32px no matter how deeply the element is nested. Because it respects the font size a user chooses in their browser settings, rem is the recommended unit for font sizes and most spacing.
Setting html { font-size: 10px; } to make maths easier overrides users who enlarge their default text for readability. Leave the root size alone and think in rem: 1rem = 16px, 1.5rem = 24px, 0.875rem = 14px.
Viewport units
Viewport units are relative to the browser window (the viewport):
1vwis 1% of the viewport's width;1vhis 1% of its height.vminandvmaxuse the smaller or larger of the two dimensions.- On phones, the address bar grows and shrinks as you scroll, which makes
100vhslightly taller than the visible area. The newer unitssvh(small),lvh(large) anddvh(dynamic) fix this.min-height: 100dvhis a reliable full-screen hero section.
Other useful relative units
ch: roughly the width of the "0" character in the current font.max-width: 65chkeeps lines of text at a comfortable reading length.fr: a fraction of free space, used only in CSS Grid (see CSS Grid).
Which unit when?
| Use | Good choice | Why |
|---|---|---|
| Font sizes | rem | Consistent and respects user settings |
| Padding and margins | rem, or em for components | Spacing scales with text |
| Borders, hairlines, shadows | px | Small precise details |
| Widths of columns and images | %, fr, max-width | Adapts to the container |
| Text column width | ch | Readable line length |
| Full-screen sections | dvh, vw | Relative to the screen |
Unitless values and calc()
Zero needs no unit: margin: 0. line-height is best written without a unit, such as line-height: 1.5, so it scales with each element's font size. When you need to mix units, calc() does the maths for you:
.sidebar {
width: calc(100% - 2rem); /* full width minus 2rem */
}
Spaces around + and - inside calc() are required. The related function clamp() is covered in Modern CSS Features.
Try it
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Units</title>
<style>
body { font-family: system-ui, sans-serif; margin: 1rem; }
.box {
background: #dbeafe;
margin-bottom: 1rem;
padding: 0.5rem;
}
.px { width: 200px; }
.pct { width: 50%; }
.vw { width: 30vw; }
.em-parent { font-size: 1.25em; }
.em-parent .em-parent { background: #fde68a; }
.rem { font-size: 1.25rem; }
</style>
</head>
<body>
<div class="box px">200px wide</div>
<div class="box pct">50% of the body</div>
<div class="box vw">30% of the window</div>
<div class="em-parent">1.25em
<div class="em-parent">1.25em again (bigger!)</div>
</div>
<p class="rem">1.25rem stays the same anywhere</p>
</body>
</html>
Three pale blue bars appear: one fixed at 200px, one half the width of the page and one 30% of the window. Resize the preview and only the last two change. Below them, the nested "1.25em again" text (on a yellow background) is visibly larger than its parent (20px × 1.25 = 25px), while the rem paragraph stays at 20px.
Key points
pxis absolute; use it for borders and fine details.remis relative to the root font size (16px by default) and is ideal for text and spacing.emis relative to the parent's (or own) font size and compounds when nested.%,vw/vh/dvhandchlet layouts adapt to containers, screens and text.
Tip: use the ← and → keys to move between pages.
