Skip to content
tezvyn:
Menu

Relative CSS Units: rem vs. em

Source: developer.mozilla.orgMediumHow cards are made

Relative CSS Units: rem vs. em

Relative CSS units like rem and em tie element sizes to font size for scalable UIs. rem is relative to the root font size, providing consistency. em is relative to the parent, causing sizes to compound. The footgun is accidentally nesting em units.

Why it exists

Hardcoding UI dimensions with absolute units like pixels (px) creates rigid designs. When a user increases their browser's default font size for accessibility, a pixel-based layout doesn't adapt, leading to broken UIs and poor readability. Relative units were created to build fluid layouts that respect user preferences.

The mental model

Think of rem as a global sizing variable and em as a local one. rem (root em) bases its size on one single source of truth: the root <html> element's font size. em bases its size on its immediate parent's font size. This makes rem predictable and em contextual.

How it works

The browser calculates the final pixel value of a relative unit. For rem, it multiplies the value by the root element's font size (e.g., 2rem with a 16px root font becomes 32px). For em, it multiplies the value by the computed font size of the element's direct parent. If that parent also uses em, the calculation compounds. For example, a child with font-size: 0.8em inside a parent with font-size: 0.8em will have a final font size of 0.64 times its grandparent's font size.

When to use it

Use rem for most of your sizing needs: global typography, margins, padding, and layout dimensions. This ensures consistency and simplifies maintenance. Use em sparingly for properties on a component that should scale relative to that component's own font size, such as the padding on a button or the size of an icon next to text.

When not to use it

Avoid using em for spacing and layout in deeply nested components. The compounding effect makes sizes difficult to predict and debug. If you find yourself calculating 1 / 1.2 * 0.8 to figure out a font size, you should probably be using rem instead. For elements that must be a fixed size, like a 1px border, px is still the correct choice.

One canonical example

Assume the browser default font-size is 16px. If you set div { font-size: 1.25rem; }, that div's font size becomes 20px (1.25 16). If a inside that div has font-size: 0.8em, its font size will be 16px (0.8 20). If that same instead had font-size: 0.8rem, its font size would be 12.8px (0.8 * 16), ignoring its parent's size completely.

Interview question

When setting global layout dimensions like margins and padding, what is the key advantage of "rem" over "em"?

  • a."em" allows elements to scale proportionally to their own font size.
  • b."rem" values are always larger, making elements more prominent.
  • c."rem" scales consistently from the root element, preventing size compounding in nested structures.Correct
  • d."em" provides more granular control over individual element spacing.
Why?

The card explains that "rem" bases its size on the root element's font size, providing consistency and predictability across the UI and avoiding the compounding effect that "em" can cause in nested layouts. While "em" does scale proportionally to its parent's (or its own) font size, this behavior makes it unsuitable for consistent global spacing in complex, nested UIs.

Just read this? Test yourself on what you have been reading.

Read the original → developer.mozilla.org

Put your scrolling time to good use

Learn one idea, try a quiz and save useful cards for revision. Tezvyn makes it easy to learn and stay current in your tech field, a few minutes at a time.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on css — each one lists the topics its interview covers.

See open roles