Almost every layout decision in CSS is a size: how big is the heading, how wide is the sidebar, how much space sits between two cards. CSS lets you express a size in many units: px, em, rem, %, vw, vh and more.
Choosing the right unit is the difference between a page that looks fine only on your laptop and one that adapts to phones, large monitors and users who have increased their browser's text size. In this chapter you will learn what each unit is relative to, how to calculate the final pixel value, and which unit to choose for which job.
Lengths, Numbers and Percentages
A CSS length is a number immediately followed by a unit, with no space:
.box {
width: 300px; /* correct */
margin: 2rem; /* correct */
padding: 10 px; /* WRONG: space between number and unit, declaration ignored */
height: 200; /* WRONG: missing unit, ignored */
}The only length that may be written without a unit is zero: margin: 0; is fine, because zero pixels and zero ems are the same thing.
Units fall into two families:
| Family | Meaning | Examples |
|---|---|---|
| Absolute | Fixed size, does not depend on anything else on the page | px, cm, mm, in, pt, pc, Q |
| Relative | Calculated from something else: a font size, the parent, the viewport | em, rem, %, ch, lh, vw, vh, dvh |
Absolute Units
All absolute units are tied together by one definition: 1 inch = 96 px = 2.54 cm.
| Unit | Name | Equals |
|---|---|---|
px | Pixel | 1/96 of an inch |
in | Inch | 96px = 2.54cm |
cm | Centimetre | 96/2.54 ≈ 37.8px |
mm | Millimetre | 1/10 cm ≈ 3.78px |
Q | Quarter-millimetre | 1/40 cm ≈ 0.945px |
pt | Point | 1/72 inch = 96/72 = 4/3 px ≈ 1.333px |
pc | Pica | 12pt = 1/6 inch = 16px |
Worked examples
12pt = 12 × 4/3 px = 16px
1pc = 12pt = 16px
2.54cm = 1in = 96px
5mm = 0.5cm = 0.5 × 37.795 ≈ 18.9px
24px = 24 × 3/4 pt = 18ptWe checked these in Chrome: width: 1in, width: 2.54cm both give 96px, and 12pt and 1pc both give 16px.
A CSS pixel is not a screen pixel
This is a common interview question. A CSS px is a reference pixel, an abstract unit roughly the size of one dot on an ordinary desktop screen viewed at arm's length. A modern phone may use 3 physical dots for each CSS pixel in each direction. JavaScript exposes this ratio as window.devicePixelRatio (for example 1 on an old monitor, 2 or 3 on phones). That's why a 16px font looks about the same size on a laptop and on a high-resolution phone, even though the phone has far more physical pixels.
Because of this, cm and in rarely match a real ruler on screen. Use them only in print style sheets where physical size actually matters. On screen, of the absolute units, only px is commonly used.
Font-Relative Units: em and rem
em
1em equals the computed font size of the current element. When used in the
font-sizeproperty itself, 1em equals the parent's font size.
This "it depends on which property" rule confuses many beginners, so let's work through it.
.parent { font-size: 20px; }
.child {
font-size: 1.5em; /* 1.5 × parent's 20px = 30px */
padding: 1em; /* 1 × own font size 30px = 30px */
width: 10em; /* 10 × 30px = 300px */
}<div class="parent">
<div class="child">Hello</div>
</div>Chrome's computed values:
font-size: 30px
padding: 30px
width: 300pxStep by step: first the browser computes the child's font-size (using the parent's 20px), and then every other em on the child uses the child's own new size, 30px.
This is useful: if you size a button's padding in em, a bigger button font automatically gives proportionally bigger padding.
The compounding problem
Because em font sizes multiply down the tree, nesting can produce surprising results:
ul { font-size: 16px; }
ul ul { font-size: 1.2em; }<ul>
<li>Level 1
<ul>
<li>Level 2
<ul>
<li>Level 3
<ul>
<li>Level 4</li>
</ul>
</li>
</ul>
</li>
</ul>
</li>
</ul>Level 1: 16px
Level 2: 16 × 1.2 = 19.2px
Level 3: 19.2 × 1.2 = 23.04px
Level 4: 23.04 × 1.2 = 27.648pxChrome computes exactly 27.648px for level 4. Every nested list grows larger, which is almost never what you want. This problem is why rem was introduced.
rem
1rem equals the font size of the root element (
<html>).
"rem" means "root em". Because it always refers to the same element, it does not compound:
.parent { font-size: 20px; }
.child { font-size: 1.5rem; } /* 1.5 × 16px = 24px, parent's 20px is ignored */With the browser default root size of 16px, the child is 24px (verified in Chrome).
Why rem and em respect the user
Every major browser uses a default font size of 16px, and users can change it in settings (people with weak eyesight often choose 20px or more). If you write font-size: 16px on your text, you override their choice. If you write 1rem, the text grows when they increase the default. This is a real accessibility benefit.
User setting 1rem 1.5rem 16px
16px (default) 16px 24px 16px
20px 20px 30px 16px ← px ignores the user's choiceA practical rule
Use
remfor font sizes and most spacing, for consistency.Use
emwhere a size should scale with the element's own text, such as button padding or icon size next to text.Use
pxfor things that should not scale with text: 1px borders, hairlines, small shadows.
Other Font-Relative Units
| Unit | Relative to | Typical use |
|---|---|---|
ch | Width of the "0" (zero) character in the element's font | Readable line lengths: max-width: 65ch |
ex | x-height of the font (height of a lowercase x), often about 0.5em | Fine typographic alignment |
cap | Height of capital letters | Sizing icons to match capitals |
ic | Width of the CJK character "水" | Chinese/Japanese/Korean text |
lh | The element's computed line-height | Spacing in multiples of a line: margin-block: 1lh |
rlh | The root element's line height | Consistent vertical rhythm |
Several of these also have root versions (rch, rex, rcap, ric), just like rem. The lh unit has been supported in all major browsers since late 2023.
The ch unit depends on the font: in Chrome on Windows, 10ch in a 16px monospace font measured about 88px. In a monospace font every character is the same width as "0", so 20ch fits exactly 20 characters. In proportional fonts it is only an approximation, but max-width: 60ch to 75ch is a well-known way to keep paragraphs comfortable to read.
Percentages
A percentage is always relative to something, but what depends on the property. This table is worth memorising:
| Property | % is relative to |
|---|---|
width, left, right | Width of the containing block (usually the parent's content box) |
height, top, bottom | Height of the containing block (only works if that height is definite) |
margin and padding (all four sides) | Width of the containing block (its inline size), even for top and bottom |
font-size | Parent's font size |
line-height | Element's own font size |
transform: translate() | The element's own size |
.container { width: 400px; }
.inner {
width: 50%; /* 50% of 400px = 200px */
padding-top: 10%; /* 10% of the container's WIDTH = 40px */
}Chrome computes width: 200px and padding-top: 40px. The fact that vertical padding uses the width surprises many people; the next chapters, The Box Model and Margin & Padding, look at it again.
A classic trap: height: 50% does nothing if the parent's height is auto (determined by its content), because 50% of "depends on the content" cannot be calculated. The percentage then behaves like auto.
Viewport Units
The viewport is the visible area of the browser window. Viewport units size things relative to it:
| Unit | Equals |
|---|---|
vw | 1% of the viewport width |
vh | 1% of the viewport height |
vmin | 1% of the smaller of width and height |
vmax | 1% of the larger of width and height |
vi, vb | 1% of the viewport in the inline / block direction (same as vw / vh for normal horizontal text) |
Worked example
On a laptop viewport of 1280 × 720:
50vw = 0.50 × 1280 = 640px
100vh = 720px
10vmin = 0.10 × min(1280, 720) = 72px
10vmax = 0.10 × 1280 = 128pxRotate a phone and vmin/vmax swap meanings, which makes them handy for elements that should fit in both orientations.
The mobile 100vh problem and svh, lvh, dvh
On mobile browsers the address bar slides away as you scroll, so the viewport height changes. Plain vh is based on the largest viewport (address bar hidden), so a height: 100vh hero section is taller than the screen when the page first loads and its bottom is hidden behind the toolbar. The newer units fix this:
| Unit | Based on | Use when |
|---|---|---|
svh | Small viewport: browser toolbars fully shown | You need the content to be fully visible on first load |
lvh | Large viewport: toolbars hidden | Same as classic vh |
dvh | Dynamic: changes live as toolbars appear/disappear | Full-screen app layouts; may cause reflow while scrolling |
Matching svw, lvw, dvw (and vmin/vmax, vi/vb variants) also exist. These units have shipped in all major browsers since 2022 and are now considered widely available, so they are safe to use:
.hero {
min-height: 100vh; /* fallback for very old browsers */
min-height: 100svh; /* fits the screen on first load on mobile */
}The 100vw scrollbar trap
vw includes the vertical scrollbar on desktop browsers that show one. So an element with width: 100vw on a page that scrolls is slightly wider than the space available and causes a horizontal scrollbar. For a full-width block, simply use width: 100% or leave width as auto.
There are also container query units (cqw, cqh, cqi...) that work like viewport units but relative to a container. They are covered in Container Queries.
line-height: Why Unitless Is Best
line-height accepts a plain number with no unit, and this is one of the most important unit lessons in CSS. Compare:
.a { font-size: 20px; line-height: 1.5; } /* unitless */
.b { font-size: 20px; line-height: 1.5em; } /* with em */
.a .big, .b .big { font-size: 40px; }<div class="a"><div class="big">Big text in A</div></div>
<div class="b"><div class="big">Big text in B</div></div>Chrome computes the line height of the 40px children as:
A child: 60px (1.5 × its own 40px)
B child: 30px (inherited 1.5em was already turned into 1.5 × 20 = 30px)Why? As you learned in Specificity, Inheritance & the Cascade, children inherit the computed value. For 1.5em, the computed value is a fixed length (30px), so the 40px child gets lines only 30px tall and its text overlaps. For the unitless 1.5, the computed value stays the number 1.5, so each child multiplies it by its own font size. Always use unitless line-height.
Doing Maths: calc()
calc() lets you mix units, and the browser does the arithmetic at layout time:
.container { width: 400px; }
.content { width: calc(100% - 2 * 20px); } /* 400 − 40 = 360px */
.sidebar-layout main {
width: calc(100% - 250px); /* everything except a fixed 250px sidebar */
}
h1 {
font-size: calc(1.5rem + 2vw); /* grows smoothly with the viewport */
}Rules to remember:
+and-must have spaces around them.calc(100%-20px)is invalid, because-20pxwould be read as a negative number.*and/don't need spaces, but adding them is good style.The result must have the type the property expects.
calc(10px * 2)is a length, so it works forwidth;calc(10px * 2px)would be an "area", so it is invalid. For maximum browser support, multiply or divide lengths only by plain numbers, as incalc(100px / 2). (Newer CSS also allows dividing a length by a length to get a plain number, such ascalc(100vw / 1px), but this "typed arithmetic" is recent and not yet supported everywhere.)calc()can be nested and can use custom properties:calc(var(--gap) * 2).
Related functions min(), max() and clamp() pick between values, for example width: min(100%, 600px) means "full width, but never more than 600px". They are covered in depth in Modern CSS: :has(), Nesting, clamp() & Logical Properties.
Non-Length Units You Will Meet
| Type | Units | Example |
|---|---|---|
| Angle | deg, rad, grad, turn | rotate(45deg), rotate(0.25turn) (= 90deg) |
| Time | s, ms | transition: color 0.3s (= 300ms) |
| Resolution | dpi, dpcm, dppx (alias x) | @media (resolution >= 2dppx) |
| Flexible length | fr | grid-template-columns: 1fr 2fr (see CSS Grid) |
Which Unit Should I Use?
| Job | Recommended | Why |
|---|---|---|
| Font sizes | rem | Respects user settings, no compounding |
| Spacing (margin, padding, gap) | rem, or em for text-linked components | Scales with text |
| Borders, hairlines | px | Should stay crisp and thin |
| Fluid widths | %, fr, min() | Adapt to the container |
| Readable text column | ch (max-width: 65ch) | Based on characters per line |
| Full-screen sections | svh / dvh | Correct on mobile |
| Line height | Unitless number | Inherits correctly |
| Print style sheets | cm, mm, pt | Paper has real physical size |
Common Mistakes
Space between number and unit (
10 px) or a missing unit (width: 200). Both make the declaration invalid.Using
emfor nested font sizes and getting ever-growing text. Userem.Forgetting that
emon padding uses the element's own font size, not the parent's.Expecting
padding-top: 10%to use the parent's height. It uses the width.height: 100%on an element whose parent has no definite height. It has no effect.width: 100vwcausing horizontal scroll because it includes the scrollbar.height: 100vhon mobile hiding content behind the toolbar; usesvhordvh.calc(100%-20px)without spaces around the minus sign.line-heightwithpxoremon a parent, causing overlapping lines in children with bigger text.
Interview / Exam Questions
1. The root font size is 16px, a parent has font-size: 1.25rem, and its child has font-size: 1.5em; padding: 0.5em. What are the child's font size and padding in px?
Parent = 1.25 × 16 = 20px. Child font size = 1.5 × 20 = 30px. Padding uses the child's own font size: 0.5 × 30 = 15px.
2. What is the difference between em and rem?
em is relative to the element's own font size (or the parent's when used in font-size), so nested values compound. rem is always relative to the root <html> font size, so it is predictable and doesn't compound.
3. Convert 18pt to pixels.
1pt = 4/3 px, so 18 × 4/3 = 24px.
4. A 600px-wide parent contains a child with margin-top: 5%. What is the margin in px?
Vertical margins (and padding) percentages use the containing block's width: 5% × 600 = 30px.
5. Why is unitless line-height recommended?
Children inherit the computed value. A unitless number is inherited as a ratio and recalculated against each child's font size; an em or px value is inherited as a fixed length, which can be too small for children with larger text.
6. Why can height: 100vh be a problem on phones, and what is the fix?
vh uses the large viewport (toolbars hidden), so on load the element is taller than the visible area. Use 100svh (small viewport) or 100dvh (dynamic) instead.
7. Is 1 CSS pixel always 1 physical pixel?
No. A CSS pixel is a reference unit (1/96 inch). On high-density screens one CSS pixel is drawn with several device pixels; the ratio is window.devicePixelRatio.
Summary
Absolute units are linked by 1in = 96px = 2.54cm = 72pt; on screen only
pxis commonly used, and it is a reference pixel, not a hardware pixel.emis relative to the element's font size (parent's when infont-size) and compounds when nested;remis relative to the root and doesn't.Percentages depend on the property; padding and margin percentages always use the containing block's width.
vw/vhare relative to the viewport; usesvh/dvhfor mobile full-height layouts, and avoid100vwfor full-width blocks.Use unitless
line-height,remfor text,pxfor hairlines andchfor line length.calc()mixes units; keep spaces around+and-.
With colours and units in hand, we can now look at how every element is sized and spaced in The Box Model.