When should I use px, em, or rem for CSS values?
Quick answer
Use rem for font sizes and spacing that should share a consistent scale from the root font size; use em when a value should follow the element’s text size, such as a component’s padding. Use px when you want a fixed CSS reference length. A CSS pixel is not necessarily one physical screen pixel, and relative units resolve according to font context. [S1] [S2]
The question, the details, the next step.
A practical checklist
Decide whether each value should stay fixed, follow component text, or follow the root scale.
Use rem for typography or spacing intended to share a root-based scale.
Use em when a component length should track its own text size.
Inspect nested em font sizes for compounding.
Check the root and relevant element or parent font sizes before calculating values.
A few more useful answers
Does 1px in CSS always mean one pixel on my screen?
Is the root font size always 16px?
No. 16px is common, but not guaranteed; rem resolves relative to the root element’s font size. [S2]
Why can nested em text become unexpectedly large?
For font-size, em uses the parent’s computed font size, so nested relative sizes can multiply. Using rem can avoid that parent-by-parent compounding. [S1]
Sources
References used for this guide.
- [S1] font-size CSS property — MDN developer.mozilla.org · Checked
- [S2] <length> CSS type — MDN developer.mozilla.org · Checked
