- Published on
Understanding the Difference Between em and rem in CSS
- Authors
- Name
- Hieu Cao
em
and rem
in CSS
Understanding the Difference Between 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
em
?
What is 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
.
rem
?
What is 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.
em
and rem
When to use em
?
When to use - 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.
rem
?
When to use - 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.
em
and rem
Comparison of Feature | em | rem |
---|---|---|
Depends On | Font size of the nearest parent element | Font size of the root element <html> |
Flexibility | Flexible for nested designs | Ideal for consistent designs |
Complexity | Can be tricky with deep nesting | Easier to predict |
Use Case | Modular components adapting to context | Global styles and consistent layouts |
Example | Padding or font size that changes with the parent | Static 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.