A CSS length is a number with a unit. It sets a size: width, height, margin, padding, or font-size. 200px is a length. 50% is a percentage of another size. 2em and 2rem follow a font size. 10vh is one tenth of the viewport height, and 10vw is one tenth of the viewport width.
Absolute lengths do not depend on another element: px, cm, mm, Q, in, pc, and pt. px is the one you use on screen. The others show up more often in print. Relative lengths are calculated from something else. % follows the parent. em and rem follow a font size. vw, vh, vmin, and vmax follow the viewport.
A length other than 0 needs a unit.
width: 200 is invalid. width: 200px is a length. 0 is allowed without a unit because zero is zero in every unit.
Syntax
CSS Syntax
The unit is written against the number, with no space:
CSS
.fixed {
width: 200px; /* absolute: stays 200px */
}
.half {
width: 50%; /* half of the parent width */
}
.grow {
font-size: 2em; /* twice the parent font size */
}
.root {
font-size: 2rem; /* twice the root font size */
}
.wide { width: 10vw; } /* 10% of the viewport width */
.tall { height: 10vh; } /* 10% of the viewport height */
Reference
Core Ideas
The lengths a CSS length tutorial uses first:
Idea
Example
Remember
Absolute
200px
The parent’s size does not change it
Pixel
1px
1/96 of an inch in CSS
Percent
50%
Half the parent, for width
em
2em
On font-size, twice the parent. Nesting compounds
rem
2rem
Twice the root. Nesting stays the same
vw
10vw
10% of the viewport width
vh
10vh
10% of the viewport height
vmin
10vmin
10% of the smaller side, width or height
vmax
10vmax
10% of the larger side, width or height
Absolute length units
These seven units are all absolute. 1px is 1/96 of an inch in CSS. On a dense screen that pixel can cover more than one physical pixel.
Unit
Name
Equals
px
Pixels
1/96 of an inch
in
Inches
96px
cm
Centimeters
37.8px
mm
Millimeters
1/10 of 1cm
Q
Quarter-millimeters
1/40 of 1cm
pc
Picas
1/6 of 1in
pt
Points
1/72 of 1in
Code
CSS Examples
Four lengths. View Output shows the size the browser computes. Try It Yourself opens the same example in the editor.
Example 1 — A fixed width in px
width: 200px is an absolute length. The box stays 200px wide inside a larger parent.
1vh is 1% of the viewport height. 10vh is one tenth of the window.
1vw is 1% of the viewport width. 10vw is one tenth of the window.
vmin uses the smaller of those two sides. vmax uses the larger one. Resize the window and all four change. In Try It Yourself, the preview pane is the viewport.
Frequently Asked Questions
A length is a number with a unit, used for width, height, margin, padding, and font-size. Absolute lengths are px, cm, mm, Q, in, pc, and pt. Relative lengths are calculated from something else: % from the parent, em from a font size, rem from the root, and vw, vh, vmin, and vmax from the viewport.
px is an absolute length. 1px is 1/96 of an inch in CSS, so 200px is a fixed width. It does not grow when the parent grows. On a high-density screen, 1px may cover more than one physical pixel, and the size is meant to look similar at a normal viewing distance. cm, mm, Q, in, pc, and pt are absolute too. On screen, px is the one you write.
On font-size, em is a multiple of the parent font size. A nested 2em is twice the already enlarged parent, so the sizes compound. rem is always a multiple of the root font size. Nested 2rem stays the same size as the first 2rem.
For width, a percentage is a share of the parent’s width. A child with width: 50% inside a 320px parent is 160px wide. The same 50% inside a wider parent is wider. It is not 50% of the viewport.
1vh is 1% of the viewport height, the browser window’s height. 1vw is 1% of the viewport width. 10vh is one tenth of the window height, and 10vw is one tenth of the window width. They follow the window, not the parent. Resize the window and the box changes.
1vmin is 1% of the smaller viewport side. If the window is wider than it is tall, vmin follows the height, the same side as vh. 1vmax is 1% of the larger side. If the window is wider than it is tall, vmax follows the width, the same side as vw.