Emmanuel Akinfulubi

HTML and CSSHTML and CSS

CSS units: px vs em vs rem vs ch

23 em, rem and the rest

fair

Size the same thing in px, em, rem, % and ch, and work out what each resolves to when the root is 16px and the parent is 20px.

Stuck? Three hints, smallest first
  1. 1. rem is always relative to the root font size. em is relative to the element’s own parent.
  2. 2. Nest two em sizes and they multiply. That is the compounding people get caught by.
  3. 3. ch is the width of a “0” in the current font, which makes it the honest unit for line length.

index.html

<div class="parent">
  parent, font-size 20px
  <p class="in-em">1.5em &rarr; 30px, because em looks at the parent</p>
  <p class="in-rem">1.5rem &rarr; 24px, because rem looks at the root</p>
  <p class="in-ch">max-width 30ch, a measure that tracks the font</p>
</div>

style.css

:root { font-size: 16px; }

.parent {
  font-size: 20px;
  padding: 12px;
  border: 1px solid #c9cad1;
  border-radius: 8px;
  background: #f7f7f8;
}

.in-em  { font-size: 1.5em; }    /* 1.5 x 20 = 30px */
.in-rem { font-size: 1.5rem; }   /* 1.5 x 16 = 24px */
.in-ch  { max-width: 30ch; }

.parent p { margin: 0 0 8px; }
Rendered result. Shown, not machine-checked: there is no browser in the build.
7 checks that prove this answer

Assertions, run in the browser. The markup and styles above are handed to them as html and css, so press Run and each check prints ok. The build fails if a single one fails.

Checks for exercise 23

const root = Number(/:root\s*\{\s*font-size:\s*(\d+)px/.exec(css)[1]);
const parent = Number(/\.parent\s*\{\s*font-size:\s*(\d+)px/.exec(css)[1]);
const emFactor = Number(/\.in-em\s*\{\s*font-size:\s*([\d.]+)em/.exec(css)[1]);
const remFactor = Number(/\.in-rem\s*\{\s*font-size:\s*([\d.]+)rem/.exec(css)[1]);

test('the root is 16px and the parent 20px', [root, parent], [16, 20]);
test('1.5em resolves against the parent, giving 30px', emFactor * parent, 30);
test('1.5rem resolves against the root, giving 24px', remFactor * root, 24);
test('so the same factor produces different sizes', emFactor * parent === remFactor * root, false);
test('a nested 1.5em would compound to 45px', emFactor * emFactor * parent, 45);
test('the measure is set in ch, not px', /max-width:\s*30ch/.test(css), true);
test('the root size is stated rather than assumed', /:root/.test(css), true);

Output


      

The fifth check is the one that bites in real code: em compounds. Two nested elements at 1.5em land at 45px from a 20px parent, so a component that looked right on its own grows every time it is nested one level deeper. rem sidesteps that entirely by always measuring from the root, which is why it is the default choice for type and spacing, with em kept for things that genuinely should scale with their own text — padding inside a button, for instance. Never set the root size in px in production, though: it overrides the reader’s browser font setting.

Newsletter

New writing, when it lands.

Occasional notes on shipping software with AI agents. No schedule, no filler, unsubscribe in one click.