The Basics: Understanding Relative Units in CSS
In modern web development, choosing the right CSS units directly impacts accessibility, maintainability, and user experience. Relative units like rem and em have become the foundation of scalable, responsive designs because they adapt to user preferences and system settings.
The browser's default font size is typically 16 pixels, which serves as the reference point for both rem and em calculations. When users adjust their browser's font size setting--often to improve readability--relative units ensure your design scales proportionally without requiring code changes.
What You Will Learn
- The fundamental difference between rem and em units
- How root-based scaling with rem improves accessibility
- When to choose em for component-local flexibility
- Performance implications and browser rendering considerations
- Practical code patterns for Next.js applications
By mastering these relative units, you can build interfaces that respect user preferences while maintaining consistent, predictable behavior across your entire web development project. Whether you're building a marketing site, a SaaS application, or an e-commerce platform, these principles apply universally.
TL;DR: This guide covers CSS rem and em units, explaining how they differ, when to use each, and how they contribute to accessible, maintainable web interfaces.
The rem unit offers predictable, accessible sizing across your entire application
Root Reference Point
rem is always calculated relative to the font size of the root HTML element, regardless of where in your document tree an element appears.
Predictable Calculations
With a 16px root, 1rem equals 16px, 2rem equals 32px. This linear relationship makes rem values easy to reason about and maintain.
Global Consistency
Every rem value in your stylesheet calculates from the same base, simplifying responsive adjustments and design system maintenance.
Accessibility Built-In
rem units respect the user's browser font size setting, ensuring your typography is readable for users who need larger text.
1/* Base setup - respect user preferences */2html {3 font-size: 100%; /* Browser default, usually 16px */4}5 6h1 {7 font-size: 2.5rem; /* Equals 40px */8}9 10p {11 font-size: 1rem; /* Equals 16px */12}13 14.card {15 padding: 1.5rem; /* Equals 24px */16}Understanding CSS em: Parent-Relative Scaling
The em unit is relative to the font size of the element itself, or in the case of font-size properties, the parent element. This makes em inherently contextual--its computed value depends on where it's used in your HTML structure.
The Compound Effect
While this might seem similar to rem at first glance, the critical difference emerges with nested elements. When em values compound through multiple levels of nesting, they can produce unexpectedly large or small results:
.parent {
font-size: 1.5em; /* 24px if root is 16px */
}
.child {
font-size: 1.5em; /* 36px (1.5 × 24px) */
}
.grandchild {
font-size: 1.5em; /* 54px (1.5 × 36px) */
}
When em Makes Sense
Use em when you want component-local scaling where all internal measurements should adjust together:
- Icon buttons: Icons scale proportionally with surrounding text
- Card components: Padding scales with the card's base font size
- Nested UI patterns: Components used at different sizes throughout an application
- Proportional scaling: Cases where child elements should scale with their parent
This same property makes em valuable for component-specific scaling. When you want a button's padding, text size, and icon to scale together based on the button's own font size, em provides exactly that behavior--ideal for building consistent component libraries across your application.
For deeper insights on modern CSS techniques, explore our guide to CSS container queries which complement relative units for responsive layouts.
Key Insight: The compound behavior is both the power and the danger of em units. Use it intentionally for controlled proportional scaling.
| Aspect | rem | em |
|---|---|---|
| Reference Point | Root HTML element font size | Parent element font size |
| Predictability | High - single reference point | Variable - depends on nesting |
| Compound Effects | None | Yes - can multiply in nesting |
| Best For | Global typography, spacing tokens | Component-local scaling |
| Accessibility | Excellent - respects user settings | Good but contextual |
| Maintenance | Simpler - change one value to scale all | Requires understanding hierarchy |
Best Practices for 2025
Typography Recommendations
The modern consensus among CSS experts is clear: use rem for typography. This recommendation stems from several factors:
- Accessibility: rem units respect the user's browser font size setting
- Consistency: A single typography scale maintains visual harmony
- Predictability: Changing root font size scales everything proportionally
Typography Scale Example
/* Modern typography scale using rem */
html {
font-size: 100%;
}
body {
font-size: 1rem;
line-height: 1.5;
}
h1 {
font-size: 2.5rem;
line-height: 1.2;
}
h2 {
font-size: 2rem;
line-height: 1.25;
}
h3 {
font-size: 1.75rem;
line-height: 1.3;
}
Component Spacing Patterns
For component architecture, a hybrid approach works best. Use rem for the component's base sizing, then em for internal measurements:
.card {
font-size: 1rem; /* Base size using rem */
padding: 1em 1.25em; /* Internal spacing using em */
margin-bottom: 1.5rem; /* External spacing using rem */
border-radius: 0.5rem; /* Consistent border radius */
}
.card-title {
font-size: 1.25rem;
margin-bottom: 0.75em; /* Scales with card font size */
}
This pattern ensures your responsive design scales predictably while maintaining proportional relationships within components. When implementing these techniques, always consider how they integrate with your overall front-end architecture for maximum maintainability.
Design System Integration
Implementing a consistent CSS unit strategy is essential for maintaining scalable design systems. Consider documenting your approach in a style guide and enforcing it through code review processes.
| Pixels | rem Value | Pixels | rem Value |
|---|---|---|---|
| 8px | 0.5rem | 24px | 1.5rem |
| 10px | 0.625rem | 32px | 2rem |
| 12px | 0.75rem | 48px | 3rem |
| 14px | 0.875rem | 64px | 4rem |
| 16px | 1rem | 96px | 6rem |
| 20px | 1.25rem | 128px | 8rem |
Common Pitfalls and How to Avoid Them
Pitfall 1: Forgetting the Root Default
Setting a fixed pixel value on the root HTML element overrides user preferences:
/* Bad - ignores user settings */
html {
font-size: 16px;
}
/* Good - respects user settings */
html {
font-size: 100%;
}
Pitfall 2: Unexpected Compound Scaling
Deeply nested em values can grow unexpectedly:
/* Problematic - each level compounds */
.level-1 { font-size: 1.2em; }
.level-2 { font-size: 1.2em; }
.level-3 { font-size: 1.2em; }
/* At level 3, text is 1.728× the base size */
Pitfall 3: Mixing Units Inconsistently
Using rem and em interchangeably without a clear strategy leads to maintenance challenges. Establish team conventions and document them in your front-end development standards.
Pitfall 4: Assuming All Elements Use the Same Root
While rare, some CSS frameworks or reset stylesheets modify the root font size, affecting all rem calculations unexpectedly. Always audit your base styles when integrating new tools into your tech stack.
These CSS fundamentals pair well with our CSS custom properties guide for building dynamic, themeable interfaces that respect user preferences.
1// In CSS-in-JS (styled-components/Emotion)2const Heading = styled.h1`3 font-size: ${props => props.theme.fontSizes.xl};4 /* theme.fontSizes.xl is defined in rem units */5`;6 7const Card = styled.div`8 padding: ${props => props.theme.space.lg};9 padding-left: 1.5em; /* Proportional internal padding */10`;11 12// Tailwind CSS uses rem internally by default13// Configuration in tailwind.config.js14module.exports = {15 theme: {16 fontSize: {17 base: '1rem',18 lg: '1.125rem',19 xl: '1.25rem',20 '2xl': '1.5rem',21 '3xl': '2rem',22 }23 }24}Conclusion
Mastering rem and em units is fundamental to building accessible, maintainable web interfaces. The key takeaways are:
- Default to rem for typography and global spacing--it provides predictability and accessibility
- Use em for component-local scaling where proportional relationships matter
- Keep the root font size at 100% to respect user preferences
- Be mindful of compound scaling with nested em values
- Apply these principles consistently across your codebase
By following these guidelines, your CSS architecture will be more maintainable, your designs more accessible, and your codebase easier to work with as it scales. These practices are essential for any modern web application that prioritizes user experience and long-term maintainability.
For teams building complex applications, establishing CSS unit conventions early pays dividends as the project grows. Consider documenting your approach in a front-end style guide and enforcing it through code review processes.
Ready to implement these practices in your next project? Our team specializes in modern web development using best-in-class CSS architecture and accessible design patterns.