CSS rem units are a powerful tool for creating scalable, maintainable, and accessible web designs. Unlike pixels or em units, rem (root em) units are relative to the root element’s font size, making them ideal for responsive typography and consistent spacing throughout your website.
The rem unit stands for “root em” and represents a relative length unit in CSS. It’s calculated based on the font-size of the root element (html element) of the document. By default, most browsers set the root font-size to 16px, which means 1rem equals 16px.
Key Point: rem units always reference the root element’s font-size, unlike em units which reference the parent element’s font-size.
How rem Units Work
The calculation for rem units is straightforward:
1rem = root element font-size
If root font-size is 16px: 1rem = 16px, 2rem = 32px, 0.5rem = 8px
If root font-size is 20px: 1rem = 20px, 2rem = 40px, 0.5rem = 10px
Basic rem Unit Example
CSS
/* Root element font-size */
html {
font-size: 16px; /* Base size */
}
/* Using rem units */
.heading {
font-size: 2rem; /* 32px */
margin-bottom: 1rem; /* 16px */
}
.paragraph {
font-size: 1rem; /* 16px */
line-height: 1.5rem; /* 24px */
padding: 0.5rem; /* 8px */
}
This is a 2rem heading
This paragraph uses 1rem font-size with 1.5rem line-height and 0.5rem padding.
rem vs em vs px: Understanding the Differences
Understanding when to use rem, em, or px units is crucial for effective CSS development:
rem units have excellent browser support and are widely adopted:
Modern browsers: Full support in all current browsers
Internet Explorer: Supported from IE9+
Mobile browsers: Full support across iOS Safari and Android Chrome
⚠️ Legacy Browser Note: If supporting IE8 and below, provide pixel fallbacks before rem declarations.
Fallback Strategy for Legacy Browsers
CSS
/* Fallback approach */
.element {
font-size: 18px; /* Fallback for old browsers */
font-size: 1.125rem; /* Modern browsers */
padding: 16px; /* Fallback */
padding: 1rem; /* Modern */
margin: 24px 0; /* Fallback */
margin: 1.5rem 0; /* Modern */
}
Common Pitfalls and How to Avoid Them
1. Overriding Root Font-Size Inconsistently
❌ Avoid: Setting different root font-sizes in multiple places without a clear system.
CSS – Avoid This
/* Inconsistent root sizing */
html { font-size: 16px; }
@media (max-width: 768px) {
html { font-size: 14px; }
}
/* Somewhere else in CSS */
body { font-size: 18px; } /* This breaks the rem calculation */
✅ Better Approach: Establish a clear root font-size strategy and stick to it.
2. Mixing Units Inconsistently
CSS – Better Practice
/* Consistent unit usage */
.component {
/* Use rem for sizes that should scale */
font-size: 1.125rem;
padding: 1rem;
margin: 1.5rem 0;
/* Use px for things that shouldn't scale */
border: 1px solid #ddd;
box-shadow: 0 2px 4px rgba(0,0,0,0.1);
/* Use % for responsive widths */
width: 100%;
max-width: 50rem;
}
Performance and Accessibility Benefits
Accessibility Advantages
User preference respect: rem units scale with user’s browser font-size settings
Consistent scaling: All elements scale proportionally when users adjust font size
Better readability: Maintains relative proportions across different devices
CSS rem units provide a powerful foundation for creating scalable, accessible, and maintainable web designs. By using rem units consistently throughout your stylesheets, you can build responsive layouts that adapt beautifully to different screen sizes and user preferences.
Key takeaways for using rem units effectively:
Use rem for font-sizes, padding, margins, and other scalable dimensions
Establish a clear root font-size strategy with media queries
Combine rem units with CSS custom properties for advanced scaling systems
Reserve px units for borders, shadows, and elements that shouldn’t scale
Test your designs with different browser font-size settings for accessibility
Start incorporating rem units into your next project and experience the benefits of a truly scalable design system that respects user preferences and maintains consistency across all devices.