Published on

Understanding the Difference Between em and rem in CSS

Authors
  • avatar
    Name
    Hieu Cao
    Twitter

Understanding the Difference Between em and rem in CSS

In CSS, em and rem are two commonly used units for setting font sizes, spacing, and other properties. However, they often cause confusion, especially for developers new to frontend development. Let’s explore the difference between these units and how to use them effectively.


1. Definition and How They Work

What is em?

  • em is a relative unit based on the font size of the nearest parent element, or the element itself (if no parent font size is set).
.parent {
  font-size: 16px;
}
.child {
  font-size: 2em; /* 2 * 16px = 32px */
}

Here, child has a font size of 32px because it inherits the font size from parent.

  • With nested elements, em can become tricky:
.outer {
  font-size: 16px;
}
.inner {
  font-size: 1.5em; /* 1.5 * 16px = 24px */
}
.nested {
  font-size: 1.5em; /* 1.5 * 24px = 36px */
}

In this case, .nested ends up with a font size of 36px because it depends on .inner.

What is rem?

  • rem (Root EM) is a unit based on the font size of the root element (usually the <html> tag). Here, child always has a font size of 32px, regardless of the nesting level or parent elements.

When to use em and rem

When to use em?

  • Use em when you want sizes to depend on the font size of the nearest parent.
  • This is useful for modular designs, where components adapt based on their context.
.button {
  font-size: 1em; /* Inherits from the parent */
  padding: 0.5em 1em; /* Scales with font size */
}

If the parent’s font-size changes, both the font size and padding of .button adjust accordingly.

When to use rem?

  • Use rem when you want consistent sizes that are unaffected by parent elements.
  • This is ideal for global layouts or components.
html {
  font-size: 10px; /* 1rem = 10px */
}
h1 {
  font-size: 2.4rem; /* 24px */
}
p {
  font-size: 1.6rem; /* 16px */
}

Font sizes remain consistent and are easily adjustable by changing the root font-size.

Comparison of em and rem

Featureemrem
Depends OnFont size of the nearest parent elementFont size of the root element <html>
FlexibilityFlexible for nested designsIdeal for consistent designs
ComplexityCan be tricky with deep nestingEasier to predict
Use CaseModular components adapting to contextGlobal styles and consistent layouts
ExamplePadding or font size that changes with the parentStatic spacing like margins or base font size

Practical Usage Tips

Use rem for global styles

  • Example: Page-wide font sizes, padding, or margins for the main layout.

Use em for local components

  • Example: Padding, margins, or font sizes inside modular components like buttons or cards.

Combine both for flexibility

  • Use rem for core elements and em for dependent elements to create a balanced design.

Conclusions

Understanding the difference between em and rem will help you master CSS, especially in complex projects. Using them correctly not only saves time but also makes your code easier to maintain.