Full answer
Units say how big a length is. They fall into two groups: absolute and relative.
pxis a fixed CSS pixel. It does not change with other settings on the page.emis relative to the font size of the element itself. For thefont-sizeproperty, it is relative to the parent's font size, which is why it compounds when nested.remis relative to the font size of the root element (html). It does not compound, so it is predictable.%is relative to something that depends on the property. Forwidthit is the containing block's width. Forfont-sizeit is the parent's font size. Forpaddingandmarginit is the containing block's width, even for the top and bottom sides.vwandvhare 1 percent of the viewport width and height.chis the width of the digit 0 in the current font, handy for line lengths.
Computed values in Chrome: 30px, 45px, 30px, 20px padding and a width of 200px for the 50% box.
Use rem for font sizes and spacing so the page respects the font size a user sets in the browser. Use em for things that should scale with their own text, such as button padding. Use px for hairlines such as a 1px border.
html
<style>
html { font-size: 20px; }
.parent { font-size: 1.5em; } /* 30px */
.child { font-size: 1.5em; } /* 45px: 1.5 x 30 */
.rem { font-size: 1.5rem; } /* 30px: always 1.5 x 20 */
.pad { font-size: 10px; padding: 2em; } /* 20px: em uses its own font-size */
.half { width: 50%; }
</style>
<div class="parent" id="p">Parent
<div class="child" id="c">Child (em)</div>
<div class="rem" id="r">Child (rem)</div>
</div>
<div class="pad" id="pad">Padding in em</div>
<div style="width: 400px"><div class="half" id="half">Half</div></div>Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- em compounds when nested; rem always refers to the root font size.
- Percent depends on the property: width uses the containing block width, and padding and margin also use width.
- vw and vh are 1 percent of viewport width and height.
- rem respects a user's chosen default font size; px does not.
- 100vw can include scrollbar width and cause horizontal scroll.
Common mistakes
- using em for nested font sizes and getting sizes that grow at each level, such as nested lists.
- using 100vw for full width. It includes the scrollbar width on systems with classic scrollbars, which can cause a small horizontal scroll.
Follow-ups
- why does setting html { font-size: 62.5% } come up in tutorials?
- Why do some people set html font-size to 62.5 percent?
- What are dvh, svh and lvh for, and what should you check before using them?
Tips
- Prefer rem for font-size. If you use px, a user who raised their default font size in the browser settings will not see the change.
- Show one computed example, like 1.5em inside 1.5em giving 2.25 times the base size.
- Say which unit you pick for font-size, borders and component padding, and why.