What is rem vs em?
Both are relative font-size units. rem (root em) is relative to the root <html> element's font size, usually 16px, so 1.5rem is the same everywhere on the page. em is relative to the current element's own font size (or the parent's font size when used for font-size), so it compounds when nested.
Calculations
html { font-size: 16px }→2rem= 32px anywhere.- Parent
font-size: 20px, childfont-size: 1.5em→ 30px. - Nested lists with
font-size: 1.2emgrow at every level: 1.2 × 1.2 × 1.2…, the classic compounding bug. - For
padding,marginandwidth,emrefers to the element's own computed font size.
When to use which
rem: font sizes, spacing scale, layout widths and media queries. Predictable and respects the user's browser font setting.em: things that should scale with the component's own text size: button padding, icon size, letter-spacing, underline offsets. Change a button's font size and everything scales together.px: borders, hairlines and shadows that should stay crisp.
Accessibility
Users who raise their browser's default font size get bigger text only if you use relative units. Never set html { font-size: 62.5% } and then write everything in px. Avoid setting the root size in px at all.
Other relative units
ch (width of "0", great for max-width: 65ch readable lines), ex, lh/rlh (line height), and container units cqi.
Code example
1<style>2 body { font-family: system-ui; }3 .parent { font-size: 20px; border: 2px dashed #94a3b8; padding: 8px; }4 .rem { font-size: 1.5rem; } /* 24px: ignores the parent */5 .em { font-size: 1.5em; } /* 30px: 1.5 × 20px */6 ul.em-list li { font-size: 1.15em; } /* compounds with nesting */7 .btn { font-size: 1rem; padding: .5em 1em; border-radius: .4em; border: 2px solid #0b1020; background: #ffd84d; }8 .btn.large { font-size: 1.5rem; } /* padding and radius scale too */9 .prose { max-width: 45ch; }10</style>11<div class="parent">12 parent 20px13 <p class="rem">1.5rem</p>14 <p class="em">1.5em</p>15</div>16<ul class="em-list"><li>Level 1<ul><li>Level 2<ul><li>Level 3 keeps growing</li></ul></li></ul></li></ul>17<button class="btn">em padding</button>18<button class="btn large">scales with font size</button>19<p class="prose">This paragraph is limited to about 45 characters per line using the ch unit, which keeps text comfortable to read.</p>20<pre id="out"></pre>21<script>22 const px = (sel) => getComputedStyle(document.querySelector(sel)).fontSize;23 document.getElementById("out").textContent = `.rem = ${px(".rem")}, .em = ${px(".em")}`;24</script>
Practice this question
Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the HTML & CSS set.