π What Are Relative Units?
Relative units scale based on another value β usually the font size of an element or the root HTML element. They make layouts more flexible, scalable, and responsive than fixed units like px.
Note
π °οΈ em β Relative to Parent Font Size
The em unit scales based on the font size of the current element, which often inherits from its parent. This means em composes, so nested elements multiply in size.
π How em Works
em Basic Example
.parent {
font-size: 20px;
}
.child {
font-size: 2em; /* = 40px */
}2em = 2 Γ parentβs font-size (20px) = 40px.
π§ͺ Example β Nested em Multiplication
Nested em
.box {
font-size: 20px;
}
.box .inner {
font-size: 1.5em; /* = 30px */
}
.box .inner .deep {
font-size: 1.5em; /* = 45px (1.5 Γ 30px) */
}Note
π― When to Use em
- Spacing inside components (padding, margin)
- Scalable button padding
- Layouts where spacing should follow font-size
- Nested elements that scale relative to their parent
π¦ Example β Button Scaling with em
Button Example
button {
font-size: 1rem; /* text size */
padding: 0.75em 1.5em; /* padding grows along with text */
}Padding grows automatically if the button font increases.
π ±οΈ rem β Relative to Root (html) Font Size
The rem unit is based on the root elementβs font-size(usually defined on html ).
π How rem Works
rem Example
html {
font-size: 16px;
}
.title {
font-size: 2rem; /* = 32px */
}
.subtitle {
margin-bottom: 1.5rem; /* = 24px */
}rem remains consistent, even inside deep nesting.
π― When to Use rem
- Global typography
- Consistent spacing system
- Design systems and UI libraries
- Responsive scaling (changing root font size)
π± Responsive Typography with rem
Responsive rem
html {
font-size: 16px;
}
@media (max-width: 600px) {
html {
font-size: 14px; /* Smaller screens = smaller rem */
}
}Every rem-based measurement now scales automatically.
π em vs rem β Quick Comparison
| Feature | em | rem |
|---|---|---|
| Relative To | Parent font-size | Root font-size |
| Nesting behavior | Compounds | Stays consistent |
| Best Use | Component spacing | Typography & layout scale |
| Predictability | Less predictable | More predictable |
π§ͺ Practical Example β Navbar Using rem & Buttons Using em
Practical Layout
html { font-size: 16px; }
.navbar {
padding: 1.5rem;
}
.navbar h1 {
font-size: 2rem;
}
button {
font-size: 1rem;
padding: 0.75em 2em;
}Navbar spacing remains consistent site-wide (rem),
Button spacing grows with its own font size (em).
β οΈ Common Mistakes
- β Using em for global typography β results in compounding issues
- β Using rem for component padding β breaks scaling inside components
- β Forgetting that browser default font-size = 16px
- β Changing html font-size too aggressively β accessibility issues
Note
β Use rem for typography & layout spacing
β Use em for internal component spacing
π₯ Summary
em and rem are powerful units for responsive, scalable CSS. Understanding their differences helps you build predictable and flexible components while maintaining consistent typography.
Learn more βMDN Docs π