Three Units, One Underlying Question
px, rem, and em all describe a length, but they answer a different question about what that length is relative to. Getting that distinction wrong is the single most common source of "why is this element a different size than I expected" in CSS.
px → an absolute length, period. 16px is 16px everywhere, always.
rem → relative to the ROOT element's font-size. Always the same ratio, everywhere.
em → relative to the CURRENT element's own (or inherited) font-size. Compounds through nesting.
The Conversion Math
rem = px ÷ root-font-size
px = rem × root-font-size
With the browser default root font-size of 16px:
24px = 1.5rem (24 ÷ 16)
8px = 0.5rem (8 ÷ 16)
1rem = 16px (1 × 16)
If a project sets html { font-size: 62.5%; } — a common trick making the root 10px instead of 16px — the same math applies with 10 as the divisor instead: 24px = 2.4rem. Always check what the actual root font-size is before converting; assuming 16px when a project overrides it is the most common place this goes wrong.
Why em Compounds and rem Doesn't
This is the part that actually causes bugs, not just conceptual confusion.
.card { font-size: 1.2em; } /* 1.2 × parent's font-size */
.card .title { font-size: 1.2em; } /* 1.2 × .card's font-size, NOT the root */
If .card sits inside an element that's already at 1.2em, and .title is 1.2em again, the actual rendered size is 1.2 × 1.2 × root-size — each nested em multiplies against the one before it. This is called compounding, and it's the specific, well-documented reason em has a reputation for "unpredictable" sizing in deeply nested components: the same 1.2em declaration produces a different actual pixel size depending on how deeply nested the element happens to be.
.card { font-size: 1.2rem; } /* 1.2 × ROOT font-size — always */
.card .title { font-size: 1.2rem; } /* 1.2 × ROOT font-size — still always */
rem sidesteps this entirely by always measuring against the root element, no matter how deep the nesting goes. That's why rem became the default recommendation for most sizing in modern CSS — not because em is "wrong," but because rem's behavior doesn't depend on where in the DOM tree a value happens to be used.