Recent chats
ASKGEEZ Q&A GUIDETechnology & devices

When should I use px, em, or rem for CSS values?

Quick answer

Use rem for font sizes and spacing that should share a consistent scale from the root font size; use em when a value should follow the element’s text size, such as a component’s padding. Use px when you want a fixed CSS reference length. A CSS pixel is not necessarily one physical screen pixel, and relative units resolve according to font context. [S1] [S2]

LET’S WORK THROUGH IT

The question, the details, the next step.

Question

When should I use px, em, or rem for CSS values? I’m choosing units for a page’s typography and spacing.

Geez

A practical starting point is: use rem for a consistent site-wide type and spacing scale, em for sizing tied to a component’s text, and px for values you intentionally want to keep fixed in CSS reference units. The key difference is what each unit follows:

  1. px is a CSS reference length. It does not necessarily equal one physical screen pixel. [S1] [S2]
  2. em is relative to font size. For font-size, it uses the parent’s computed font size; for other properties, it uses the element’s font size. [S1]
  3. rem is relative to the root element’s font size, rather than each parent in turn. [S1] [S2]

So the choice is about what should happen when the type size changes: should the value stay fixed, follow the component, or follow the page’s root scale?

Follow-up question

Can you show the difference with actual typography values, including the nesting problem with em?

Geez

Assume the root font size is 16px for this example. That’s an assumption, not a universal rule: the root size is commonly, but not invariably, 16px. [S2]

html {
  font-size: 16px; /* example assumption */
}

.page-title {
  font-size: 2rem; /* 2 × root size: 32px in this example */
}

.card {
  font-size: 1.25rem; /* 20px in this example */
  padding: 1em;       /* follows the card's 20px font size */
}

.card__note {
  font-size: 0.8em;   /* follows the parent card's computed size */
}

With that assumed root size, the title is 32px and the card text is 20px. The card’s 1em padding follows its own 20px font size, so it is 20px in this example. The note is 0.8em of the parent’s 20px font size, or 16px. These are illustrative calculations; actual resolved values depend on the font context.

Nested em font sizes can compound: two nested 1.6em font sizes multiply rather than both referring to the root. [S1] That can make deeply nested text grow more than expected. Using rem for a shared text scale avoids that parent-by-parent compounding.

Follow-up question

Should I use rem for every margin and padding too, or is em useful beyond font-size?

Geez

You don’t need one unit for every property. Use rem when you want spacing to stay aligned with the root-based scale, such as the gap between page sections. Use em when spacing should grow or shrink with the component’s own type—for example, padding around a button label, or a small gap within a text-sized badge. For properties other than font-size, em is based on the element’s font size. [S1]

A useful test is to ask: “If this component’s text gets larger, should this spacing get larger too?” If yes, em is a reasonable fit. If no, consider rem for a shared root-based value or px for an intentionally fixed CSS reference length. Relative sizing can adapt to a reader’s chosen font size, but that does not establish how every browser or page setup will behave in every situation. [S1]

Follow-up question

Can you give me a compact rule set I can use when reviewing my CSS?

Geez

Try this review rule set:

  1. Text scale: choose rem for headings and body-size steps that should share the root scale.
  2. Component relationship: choose em where padding or another length should track that element’s text size.
  3. Fixed reference: choose px when a value should stay a particular CSS reference length, rather than scaling with font context.
  4. Nested text: check chains of em font sizes; nested values can compound. Consider rem when you want a consistent root-based size instead. [S1]
  5. Assumptions: check the root font size and the relevant parent or element font size before calculating a relative value. rem is rooted in the root element, while em depends on the applicable font context. [S1] [S2]

In the example above, the title uses rem for the page scale, card padding uses em to follow card text, and the note demonstrates why nested em deserves a deliberate check. Treat those as starting choices, not a universal design system: the right relationship depends on what should scale together.

A practical checklist

  • Decide whether each value should stay fixed, follow component text, or follow the root scale.

  • Use rem for typography or spacing intended to share a root-based scale.

  • Use em when a component length should track its own text size.

  • Inspect nested em font sizes for compounding.

  • Check the root and relevant element or parent font sizes before calculating values.

A few more useful answers

Does 1px in CSS always mean one pixel on my screen?

No. CSS px is a reference length and does not necessarily map to one physical device pixel. [S1] [S2]

Is the root font size always 16px?

No. 16px is common, but not guaranteed; rem resolves relative to the root element’s font size. [S2]

Why can nested em text become unexpectedly large?

For font-size, em uses the parent’s computed font size, so nested relative sizes can multiply. Using rem can avoid that parent-by-parent compounding. [S1]

Sources

References used for this guide.

  1. [S1] font-size CSS property — MDN
    developer.mozilla.org · Checked
  2. [S2] <length> CSS type — MDN
    developer.mozilla.org · Checked

YOUR QUESTION. YOUR DETAILS.

Let’s work through your situation.

Get a tailored answer or help with your next task. Starts at $1, with a quote before you pay.

Ask your own question
← Back to Geez at Work