liveBook · Manning

Rem

As explained in CSS in Depth, Second Edition

The CSS rem unit is a relative length unit whose value is based on the computed font size of the root element.

A `rem` (“root em”) is a CSS length measured against the root element’s computed font size. Unlike an `em`, it does not change meaning when you move a rule into a differently sized component. If the root font size is 16px, `0.8rem` computes to 12.8px wherever it appears—even in nested lists. section 2.2.2

Set a usable base size

Suppose the browser’s default is 16px and the page should start at 14px. Set `:root { font-size: 0.875em; }`. The page inherits that size, while `1rem` now follows the root’s computed size. Setting the root with `em` also respects changes to the user’s default font setting. A heading can then use `font-size: 0.8rem` without depending on its parent’s font size. section 2.3.1

Avoid shrinking the root to `0.625em` merely to make `1rem` equal 10px under a 16px default. That leaves ordinary text too small and invites repeated overrides. Choose a sensible base, then adjust relative values by how they look rather than converting every declaration back to pixels. section 2.3

Listing 2.13 Creating a larger version of the panel
.panel {
  font-size: 1rem;   #1
  padding: 1em;
  border: 1px solid #999;
  border-radius: 0.5em;
}

.panel > h2 {
  margin-top: 0;
  font-size: 0.8em;    #2
  font-weight: bold;
  text-transform: uppercase;
}

The panel uses `1rem` to establish its own predictable font size. Its `em`-sized heading, padding, and radius can then scale together when that panel’s font size changes. section 2.3.3

That distinction gives you two useful controls. Change the root font size at media-query breakpoints to scale rem- and em-based measurements across the page without rewriting each component. Or change one panel’s font size from `1rem` to `1.2rem` to enlarge that panel; use `rem` instead of `em` for any internal measurement you want to keep tied to the root. section 2.3.2 section 2.3.3

Choose units by what should move together. `rem` is a practical default for predictable font sizes; `em` suits spacing that should track a component’s own text, while pixels can suit a fine border or a gap that need not scale. If reducing a navigation link’s font size would also shrink its `em` padding, switching that padding to `rem` keeps it tied to the root instead. section 2.2.2 section 12.6.2

How CSS in Depth, Second Edition covers it

How deep it goes
Defines the root-relative calculation and works through nested lists and component resizing, without prescribing rem for every length.
Examples
Shows a nested list, a relatively sized panel, and navigation padding changed from em to rem.
What it assumes
Assumes basic CSS rules while introducing the DOM root, `:root`, and the calculation behind rem.
What you can do after
Operate/configure it by setting the root font size for page-wide scaling and selecting rem or em for the desired component behavior.
How it connects
Connects rem to em, the DOM root, inheritance, media queries, and responsive design.

Also explained in

Compare how 6 books explain rem →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.