When to Use rem Versus em in CSS

Introduction

Both rem and em are font-relative CSS length units, but they use different references. Rem means root em and follows the computed font size of the root element. Em follows the font-size context of the element where it is used; on the font-size property itself, that context comes from the inherited font size.

This distinction makes the units useful for different jobs. Rem creates a shared scale across the document, so it is a strong choice for global typography, page spacing, design tokens, and consistent layout limits. Em creates local proportionality, so it works well for button padding, inline icons, badges, and dimensions that should grow when one component's text grows.

The practical answer is not to choose one unit for the entire stylesheet. A robust system often sets component font size in rem and sizes internal component geometry in em. That combination provides global consistency and local flexibility.

The Quick Difference

UnitReferencePrimary strengthMain caution
remRoot element's computed font sizeConsistent global scaleThe root is not guaranteed to be 16px
emLocal font-size contextProportional component scalingFont sizes can compound when nested

Use rem when the design relationship belongs to the page or design system. Use em when it belongs to the current component's text.

How rem Is Calculated

If the root element computes to 16 pixels, one rem is 16 pixels, 1.5rem is 24 pixels, and 2rem is 32 pixels. A descendant's own font size does not change that reference.

html {
  font-size: 16px;
}

.title {
  font-size: 2rem; /* 32px */
}

If the root later computes to 18 pixels, the same title becomes 36 pixels. Rem is predictable because every rem shares one root reference, not because the reference is permanently 16 pixels.

How em Is Calculated

For most properties, one em equals the element's computed font size. If a button computes to 20 pixels, padding-inline: 1.5em becomes 30 pixels. A different button at 16 pixels receives 24 pixels from the same declaration.

.button {
  font-size: 20px;
  padding: 0.75em 1.5em;
}

The vertical padding is 15 pixels and horizontal padding is 30 pixels. The spacing remains proportional to local text.

The Special em Rule on font-size

An em used for font-size cannot depend on the element's final font size because that would be circular. It resolves from the inherited font size, normally supplied by the parent. If the parent is 20 pixels and the child uses font-size: 1.5em, the child computes to 30 pixels.

.parent {
  font-size: 20px;
}

.child {
  font-size: 1.5em; /* 30px */
  padding: 1em;     /* 30px */
}

After font size is calculated, the child's padding uses that child's computed 30-pixel size.

When to Use rem

Choose rem when a value should remain aligned with one application-wide scale regardless of nesting. Common examples include body and heading sizes, global spacing tokens, page gutters, card padding, section rhythm, maximum reading widths, and standard radii.

Rem is especially useful when moving a component into another parent should not unexpectedly resize it. The component still responds to root and user sizing but does not inherit a local multiplication from every ancestor.

rem for Global Typography

A root-based typography scale keeps headings and body text consistent across articles, dialogs, sidebars, and cards.

body { font-size: 1rem; }
h1 { font-size: 2.5rem; }
h2 { font-size: 2rem; }
h3 { font-size: 1.5rem; }
small { font-size: 0.875rem; }

A heading does not become larger merely because it is nested inside an element with an enlarged local font size. That stability is usually desirable for a site-wide hierarchy.

rem for Global Spacing

Root-relative spacing creates a shared vertical and horizontal rhythm. A card at padding: 1.5rem and a section at margin-block: 2rem respond to the same baseline across the application.

:root {
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
  --space-xl: 2rem;
}

Tokens are more important than the literal conversions. They name approved relationships and make broad adjustments manageable.

rem for Layout Limits

A content maximum such as max-width: 70rem connects layout measure to root scale. If the user increases default text sizing, the container can grow with the typographic system rather than remaining fixed to the same pixel width.

.article {
  width: min(100%, 70rem);
  margin-inline: auto;
}

Line length still depends on font metrics and content. Use character units or specific reading tests when exact measure is important.

Why rem Does Not Compound

Two nested elements with font-size: 2rem both refer directly to the root. If the root is 16 pixels, both compute to 32 pixels. The child's value does not multiply the parent's 32-pixel size.

.parent { font-size: 2rem; }
.child { font-size: 2rem; }

This is why rem is easier to reason about in deep content and component trees.

When to Use em

Choose em when a measurement should follow a component's own text. Common uses include button padding, badge spacing, inline icon dimensions, local border radius, notification indicators, and controls that offer small and large text variants.

Em makes the component internally proportional. Changing one font-size declaration can scale several related dimensions without duplicating variant rules.

em for Button Padding

.button {
  font-size: 1rem;
  padding: 0.75em 1.5em;
  border-radius: 0.4em;
}

.button--large {
  font-size: 1.25rem;
}

The large button increases text, padding, and radius proportionally. A minimum block size can still protect the touch target if a small font variant would otherwise make the control too short.

em for Badges and Chips

Badges often need compact spacing that follows their small local text. An em-based radius and padding maintain visual balance when the badge changes size.

.badge {
  font-size: 0.875rem;
  padding: 0.3em 0.7em;
  border-radius: 0.5em;
}

The component's font size remains on the global rem scale, while internal geometry follows the component through em.

em for Inline Icons

An icon at one em tracks the surrounding text. At 16-pixel text it is 16 by 16 pixels; at 24-pixel text it becomes 24 by 24 pixels.

.text-icon {
  inline-size: 1em;
  block-size: 1em;
  vertical-align: -0.125em;
}

The small vertical adjustment is also local to text size. Test icon alignment with different fonts and writing systems rather than assuming one offset works everywhere.

em for Measures Tied to Typography

Widths expressed in em can represent a number of local font-size units. A code input at inline-size: 12em grows when its own font grows. This is useful when the expected content length and local type size are connected.

For general page columns, rem, percentages, Grid tracks, or character units may express the relationship more clearly. Use em only when the local font is intentionally the reference.

The Nested em Compounding Effect

Em-based font sizes multiply through nesting. Starting at 16 pixels, a parent at 1.2em becomes 19.2 pixels. A child at 1.2em becomes 23.04 pixels, and a third level becomes about 27.65 pixels.

li {
  font-size: 1.2em;
}

If the selector matches every nested list item, each level grows again. Values below one create progressive shrinking. This may be intentional, but it is frequently an unexpected side effect.

Preventing Accidental em Compounding

Use rem for typography that should stay on a shared scale. Alternatively, apply the em font-size declaration only to the intended direct element and reset nested structures where appropriate.

.comment {
  font-size: 1rem;
}

.comment__meta {
  font-size: 0.875em;
}

The metadata remains locally proportional, while nested comments return to the same root-based body size.

Direct rem and em Comparison

Assume the root is 16 pixels and a card establishes a 20-pixel local font size. A title at 2rem becomes 32 pixels. The same title at 2em becomes 40 pixels because em follows the card.

html { font-size: 16px; }
.card { font-size: 20px; }
.title-global { font-size: 2rem; } /* 32px */
.title-local { font-size: 2em; }   /* 40px */

Neither result is inherently better. The design must decide whether the title belongs to the global heading scale or should enlarge with the card context.

The Strong Mixed-Unit Pattern

A reliable component pattern uses rem for the component's font-size variant and em for internal dimensions.

.control {
  font-size: 1rem;
  padding: 0.75em 1.25em;
  gap: 0.5em;
}

.control--small { font-size: 0.875rem; }
.control--large { font-size: 1.25rem; }

Variants remain aligned with the global type system while their internal geometry scales automatically.

Card Component Example

.card {
  padding: 1.5rem;
  border-radius: 0.5rem;
}

.card__title {
  font-size: 1.5rem;
}

.card__title-icon {
  inline-size: 1em;
  block-size: 1em;
}

Card spacing belongs to the global layout rhythm, the title belongs to the global type scale, and the icon follows the title locally.

Form Control Example

Inputs can use a rem font size and em padding so density variants scale together. Keep target size and native usability requirements in mind.

.field {
  font: inherit;
  font-size: 1rem;
  padding: 0.75em 1em;
  border-radius: 0.375rem;
}

.field--large {
  font-size: 1.125rem;
}

Labels, error text, and help text should use a coherent rem-based hierarchy rather than inheriting arbitrary component multipliers.

Navigation Example

Navigation text can use a shared rem size while link padding follows each link's local text. This supports compact and comfortable navigation variants without duplicating padding values.

.nav { font-size: 1rem; }
.nav__link { padding: 0.75em 1em; }
.nav--large { font-size: 1.125rem; }

Touch targets must remain sufficiently large even when compact typography is selected.

rem and Design Tokens

Root-relative values fit design tokens because they preserve one shared reference across components. Typography, spacing, and layout tokens can use rem without changing meaning when consumed inside differently sized modules.

:root {
  --text-sm: 0.875rem;
  --text-body: 1rem;
  --text-heading: 1.5rem;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 1.5rem;
}

Component tokens can then use em for proportional internal values while referencing rem defaults for global alignment.

em and Component APIs

A component can expose its font size as the primary scale input and derive internal geometry in em. Consumers change one supported variable instead of overriding several selectors.

.tag {
  --tag-font-size: 0.875rem;
  font-size: var(--tag-font-size);
  padding: 0.35em 0.75em;
  border-radius: 0.5em;
}

.tag--large {
  --tag-font-size: 1rem;
}

rem in Media Queries

Relative units used in media-query conditions are based on the initial font-size context rather than font-size declarations on page elements. An em or rem breakpoint does not use the font size of the component whose style changes.

@media (min-width: 48rem) {
  .layout {
    grid-template-columns: 16rem minmax(0, 1fr);
  }
}

This lets breakpoints respect the user agent's initial sizing context without moving recursively because the matched query changes the root declaration.

em in Media Queries

Em is also widely used for media-query breakpoints. In that context it is based on the initial font size, not a local component font. Therefore, a nested component does not alter a 48em breakpoint by changing its own font-size.

Select breakpoints where content needs a structural change, not by converting a list of device widths. Whether the notation is em or rem, continuous resizing should remain usable between breakpoints.

Container Queries and Local Responsiveness

Em does not make a component structurally respond to its container; it only follows local typography. Container queries are the correct tool when layout should change based on available component width.

.card-shell { container-type: inline-size; }

@container (min-width: 32rem) {
  .card {
    grid-template-columns: 10rem minmax(0, 1fr);
  }
}

Inside each layout mode, rem can provide global scale and em can preserve local proportionality.

rem and User Font Preferences

When authors leave the root connected to user-agent defaults, rem-based typography and spacing can respond to a user's larger default font preference. Browser zoom also enlarges both relative and pixel-based CSS dimensions, but changing the default root size particularly demonstrates rem's root relationship.

This does not guarantee accessibility. Fixed heights, narrow containers, or unbounded line lengths can still fail when rem values grow.

Avoid the 62.5 Percent Root Shortcut

Some codebases set the root to 62.5 percent so a common 16-pixel default becomes 10 pixels and 1.6rem resembles 16 pixels. The arithmetic is convenient, but it changes the baseline and can create confusing interactions with user preferences and third-party components.

Modern tools and tokens remove the need for mental conversion. Leaving the root at its expected default and reasoning in relative relationships is often simpler.

Do Not Assume 1rem Is 16px

Sixteen pixels is a common browser default, not the definition of rem. The root can be changed by author CSS or user settings. If it computes to 18 pixels, 2rem is 36 pixels.

Tests and documentation should describe rem values in terms of the root scale rather than treating one pixel conversion as permanent.

Do Not Assume 1em Is 16px

Em depends on context. In a 14-pixel badge, 1em is 14 pixels. In a 24-pixel heading, it is 24 pixels. On font-size, it is derived from the inherited value before the new size is established.

Unitless line-height Is Often Better

Although line height is related to font size, a unitless multiplier such as line-height: 1.5 generally inherits more safely than 1.5em. Each descendant calculates line height from its own font size rather than inheriting a computed length from its parent.

body {
  font-size: 1rem;
  line-height: 1.6;
}

Use rem and em for lengths, but remember that unitless syntax can better express selected proportional properties.

Margins Around Typography

A heading's margin in em scales with that heading's local size, producing larger space around larger headings. A margin in rem follows the global spacing system regardless of heading size.

Editorial typography may prefer em-based local rhythm, while a component library may prefer rem tokens for consistent spacing. Choose based on whether spacing belongs to the text itself or the page system.

Border Radius: rem or em?

Use rem when a shared radius token should remain consistent across components. Use em when the corner should grow proportionally with one component's text and padding. Buttons and badges often benefit from em; cards and panels often use rem design tokens.

Neither choice requires every component to follow the same rule. Document the intended relationship and use tokens where consistency matters.

Accessibility Considerations

Relative units support scalable interfaces, but accessible outcomes depend on layout behavior. Text must reflow at zoom, controls must retain usable target sizes, labels must wrap, and content must not disappear inside fixed-height boxes. A small font combined with em-only target sizing can still create an undersized control.

Test at 200 percent zoom, with larger default fonts, long translations, and narrow windows. Use minimum control dimensions when local scaling alone is insufficient.

Using rem and em With calc() and clamp()

Root and local font units can participate in CSS math when a design needs controlled fluidity. A page gutter can begin with a root-relative baseline and gain a viewport contribution, while a heading can stay between readable rem limits.

.page {
  padding-inline: clamp(1rem, calc(0.5rem + 3vw), 3rem);
}

.hero-title {
  font-size: clamp(2rem, calc(1.25rem + 3vw), 4rem);
}

For component internals, calculations can combine em with a small stable adjustment, but simple ratios are usually easier to understand. padding-inline: 1.25em communicates local proportionality more directly than a complex formula.

Use rem for fluid minimums and maximums when those bounds belong to the site's root scale. Use em bounds only when the allowed range should itself change with the component's local font. Always test the smallest and largest results at zoom because mathematically bounded text can still collide with a rigid container.

Migrating From Pixel-Based CSS

A migration should begin by identifying relationships, not by dividing every pixel value by 16. Global type sizes and spacing tokens are candidates for rem. Padding, gaps, and icons that should follow one component's text are candidates for em. One-pixel borders, raster-aligned details, and fixed-format media may remain in pixels.

Remove fixed heights around text before converting typography. Otherwise larger root-relative text can expose clipping without fixing the underlying layout. Convert one component group at a time, compare default rendering, then test larger fonts, zoom, and narrow containers.

Mechanical conversion can preserve the exact default screenshot while leaving a rigid architecture unchanged. The goal is not numerical equivalence at a 16-pixel root; it is correct response when the reference changes. Document each category so later code follows the same model.

Third-Party and Embedded Component Contexts

A reusable component can be placed inside a host that sets an unexpected font size. Em-based internals will intentionally follow that host unless the component establishes its own font-size baseline. Rem-based dimensions continue to follow the document root, which can make the component look too large or too small relative to inherited local text if the design mixes references carelessly.

For a portable component, set an explicit rem-based font-size token at the component root and use em internally. This creates a predictable boundary while still allowing supported size variants. If the component is supposed to blend with surrounding prose, use font-size: inherit and accept that its em geometry follows the host.

Embedded documents and iframes have their own root context. One rem inside an iframe follows that document's root, not the top-level page. Component documentation should state whether it follows the host's inherited text, the current document root, or a fixed visual asset.

Internationalization and Font Differences

Rem and em are based on computed font size, but equal font-size values do not make every typeface occupy identical visual space. Fonts have different x-heights, ascenders, character widths, and fallback behavior. A control that fits an English label can wrap when translated or when a different script uses another font.

Em-based padding preserves proportion to the nominal local size, but it cannot predict label length. Allow controls to grow, avoid fixed inline dimensions, and test long words, bidirectional text, and fallback fonts. Rem-based global type scales should include line-height and reading-measure reviews for each supported script.

Inline icons sized in em remain aligned with nominal text size, yet baseline adjustment may differ by font. Test optical alignment instead of assuming one negative vertical-align value works universally. Logical properties such as padding-inline and margin-inline complement relative sizing in right-to-left and vertical writing modes.

Team Unit Policy and Code Review

Large codebases benefit from a short unit policy that describes intent rather than imposing a universal ban. A practical policy might use rem for global typography and spacing, em for component-relative geometry, percentages and layout tracks for available space, and pixels for borders or deliberate fixed details.

During review, ask what should happen if the root font grows and what should happen if only the component font grows. If both answers are unclear, the unit choice is not yet justified. Review nested em font-size declarations, fixed heights around rem text, and components that mix a locally inherited font with rem-only internals.

Design tokens should encode the policy. Root spacing and type tokens can use rem, while component CSS derives local dimensions with em. Linting can catch disallowed unit categories in selected properties, but human review is still needed because the same unit can be correct or incorrect depending on the intended relationship.

Common Mistake: em for All Typography

Applying font-size: 1.1em at several nested article levels compounds the value. If every level should use the same size, assign one rem token or inherit from a single ancestor.

Common Mistake: rem for Every Component Detail

If a large button changes from 1rem to 1.5rem but its padding remains 1rem, the text grows while internal spacing does not. Use em where local geometry should follow component text.

Common Mistake: Confusing the em Reference

font-size: 2em follows inherited font size. padding: 2em follows the element's computed font size. Saying that em always means parent size is therefore incomplete.

Common Mistake: Mechanical px-to-rem Conversion

Replacing every pixel with rem does not make a rigid interface responsive. Fixed heights, inflexible tracks, and clipped content remain architectural problems. Convert values according to intended relationships, not through a global arithmetic rewrite.

Debugging rem and em

Use DevTools to inspect the root font size, inherited font-size chain, element computed font size, and final used dimensions. Temporarily switch an em declaration to rem to reveal whether local context causes the unexpected result.

const element = document.querySelector('.button');
const root = document.documentElement;

console.log(getComputedStyle(root).fontSize);
console.log(getComputedStyle(element).fontSize);

Check every ancestor that declares font size. The nearest visual parent is not always the source of the inherited value.

Testing Strategy

Render components at default, small, and large font variants, then nest them inside differently sized containers. Verify that rem-based values remain globally stable and em-based values scale only where intended. Test several root sizes and browser zoom levels.

Include icons, multiline labels, error messages, disabled states, touch targets, and localized content. Visual regression tests can detect compounding and spacing drift, while browser tests can confirm that controls remain visible and usable.

Practical Decision Guide

Should this value follow the site-wide scale?
  Yes -> use rem

Should this value follow this component's text?
  Yes -> use em

Should it follow available container space?
  Use %, Grid, Flexbox, or container units

Should it remain a stable fine detail?
  px may be appropriate

The unit follows the dependency. A single component can legitimately use several units when different properties respond to different references.

Do not decide from the property name alone. Padding can be rem when it belongs to the shared page rhythm or em when it belongs to a button's local scale. Border radius can follow a global token on cards and a local ratio on badges. Write the intended dependency in a short comment or token name when it is not obvious, and verify the result by changing the root and component font sizes independently in DevTools.

During maintenance, test those references separately. Increase only the root size to inspect rem relationships, then enlarge only the component font to inspect em relationships. Unexpected movement in either experiment usually reveals that a property depends on the wrong sizing context.

Common Usage Guide

RequirementTypical choiceReason
Body and heading sizesremShared typography scale
Global spacing and page paddingremConsistent root rhythm
Container maximum widthremRoot-relative layout limit
Button and badge paddingemFollows local text
Inline icon sizeemTracks surrounding type
Card radiusremShared design token
Badge radiusemLocal proportional shape

Complete Button System

.button {
  --button-size: 1rem;
  font-size: var(--button-size);
  min-block-size: 2.75rem;
  padding: 0.75em 1.5em;
  border-radius: 0.4em;
  gap: 0.5em;
}

.button--small { --button-size: 0.875rem; }
.button--large { --button-size: 1.25rem; }

.button__icon {
  inline-size: 1em;
  block-size: 1em;
}

Rem selects a globally approved font size, em scales internals, and a rem minimum protects target height. This illustrates why a mixed strategy is stronger than unit purity.

Interview Questions and Answers

What is the difference between rem and em? Rem follows the root font size, while em follows the relevant local font-size context.

Which is usually better for global typography? Rem, because nesting does not change its reference.

Which is often better for button padding? Em, because padding can scale with the button's font size.

Can em compound? Yes. Nested em-based font-size declarations multiply through inherited contexts.

Can rem compound through nesting? No. It always returns to the root reference.

Is one rem always 16 pixels? No. It depends on the root computed font size.

When should em be preferred? When a measurement should remain proportional to local component text.

What do em and rem mean in media queries? Relative query units use the initial font-size context rather than declarations on page elements.

Practical Review Checklist

  • Use rem for site-wide typography and spacing scales.
  • Use em for geometry that should follow local component text.
  • Remember the special inherited context for em font-size.
  • Review nested em typography for compounding.
  • Do not assume rem or em permanently equals 16 pixels.
  • Leave the root connected to user preferences when practical.
  • Avoid changing the root merely for easier mental conversion.
  • Use unitless line height for safer typographic inheritance.
  • Protect minimum touch targets when using em-based controls.
  • Use layout and container units for non-typographic relationships.
  • Inspect root, inherited, and computed font sizes in DevTools.
  • Test zoom, larger defaults, nesting, and localized content.

Key Takeaway

Rem and em are both relative to font size, but they communicate different ownership. Rem belongs to the root scale and provides consistent typography, spacing, and layout limits across the document. Em belongs to the local font context and provides proportional component spacing, icons, and shapes.

A strong modern pattern sets component text with rem and internal component dimensions with em. Choose each unit according to the value that should control it, then test nesting, user scaling, and real content. Consistency and flexibility come from combining the units deliberately, not from declaring one universally superior.