Relative Sizes: Em and Rem
Master CSS relative units for scalable design
CSS Unit Types Overview
Absolute Units
Fixed sizes like pixels that don't change relative to other elements. Provide precise control but lack flexibility.
Relative Units
Sizes that scale based on parent elements or root values. Enable responsive and maintainable designs.
Setup Instructions
Close All Files
Close all open files in your code editor to avoid confusion during the exercise
Open Exercise Folder
Navigate to Desktop > Class Files > Advanced HTML CSS Class and open the Helping Hands Em vs. Rem folder
Launch Files
Open volunteer.html and preview it in Chrome browser for DevTools inspection
Locate the input[type="submit"] rule and replace the pixel-based padding with em units as shown below:
input[type="submit"] {
font-size: 20px;
padding: .5em 1em;
Code Omitted To Save Space
}
Here's the mathematical relationship: with a font-size of 20px, 1em equals 20px within this element. The original padding of 10px 20px converts to:
- 10px ÷ 20px = 0.5em (vertical padding)
- 20px ÷ 20px = 1em (horizontal padding)
Save the file and reload the page in Chrome.
The Sign Me Up button should maintain its visual appearance, but now we'll see the magic of proportional scaling in action.
Ensure the Sign Me Up button is still selected with the input[type="submit"] rule visible at the top of the DevTools Styles panel.
If it's not active, CTRL–click (Mac) or Right–click (Windows) on the button again and choose Inspect Element.
Switch back to the Styles tab to continue our exploration.
Em vs Rem Trade-offs
Rem stands for 'root em' and is always relative to the page's root font-size, providing consistency throughout the entire page regardless of nesting.
Em vs Rem Units
| Feature | Em Units | Rem Units |
|---|---|---|
| Reference Point | Current element font-size | Root font-size |
| Consistency | Changes with context | Consistent throughout page |
| Best Use Case | Component scaling | Global consistency |
Implementing Rem Units
Set Root Font-Size
Define font-size on :root element using relative units to respect user preferences
Convert to Rem
Change em values to rem for elements that need consistent sizing across the page
Add Media Query
Set larger root font-size for desktop breakpoints to scale entire design
Key Takeaways

button at the top right of the DevTools panel and select Dock to right as shown below: