Relative Units: Percentages (%), REM, EM, VH, and VW
Relative Units: Percentages (%), REM, EM, VH, and VW
In the previous lesson, you saw that absolute units (like px) are rigid and fixed. But the internet is accessed on thousands of different devices: from a small 360px smartphone in your pocket to a 1080px laptop, and even a 4K living room smart TV! š±š»šŗ
How can a single website look incredible on all of them?
The answer is Relative Units! Unlike a rigid wooden ruler, relative units stretch and contract like a flexible rubber band based on the size of their parent element or the user's screen.
In this lesson, you will master the 5 core relative units:
- 1Percentages (
%) - 2
rem(Root EM) - 3
em(Element EM) - 4
vw(Viewport Width) - 5
vh(Viewport Height)
The Everyday Mental Models š§
Visual Architecture & Process Flow
How data and code flow step-by-step
1. Percentages (%)
Percentages are always calculated relative to the parent element's dimensions.
Example: Multi-Column Layout
If the parent box shrinks to 600px on a smaller screen, the sidebar automatically shrinks to 180px (30% of 600) and the main area shrinks to 420px. No broken code!
2. rem (Root EM ā The Modern Standard ā)
The letters rem stand for Root EM. It is relative exclusively to the root element of your webpage (<html>).
By default, all web browsers set root font size to 16px:
Why Developers Love rem for Fonts and Padding:
If a visually impaired student increases their phone's base text size from 16px to 20px, every single heading, paragraph, and padding on your website automatically scales up proportionally!
3. em (The Local Element Unit)
While rem always looks at the root <html>, em looks at the font size of its immediate parent or the element itself!
- On
font-size:1emequals the font size of the parent element. - On
paddingormargin:1emequals the font size of the element itself!
Where em is Useful: Auto-Scaling Buttons
Notice how the button padding automatically scales up when the font size gets bigger!
ā ļø The Danger of Nested em (The Snowball Effect):
If you nest elements inside each other using em, the sizes multiply:
- Parent:
font-size: 1.2em - Child:
font-size: 1.2em(becomes1.44em) - Grandchild:
font-size: 1.2em(becomes1.728em!)
This compound multiplication can create giant runaway text! That is why rem is preferred over em for general typography.
4. Viewport Units: vw and vh
The Viewport is the visible rectangular window of your browser (excluding browser address bars and bookmarks).
1vw= 1% of the viewport width (If screen width is 1200px,1vw = 12px).1vh= 1% of the viewport height (If screen height is 800px,1vh = 8px).
The #1 Use Case for vh: Full-Screen Hero Sections ā
Have you ever seen a website where the welcome section fills the user's screen completely?
Whether viewed on an iPhone, a tablet, or an ultrawide desktop monitor, 100vh fills the exact visible vertical window!
Cheat Sheet: Absolute vs Relative Units
| Unit | Relative To | Best Used For |
|---|---|---|
px | Physical screen pixel dots | Borders (1px solid), small shadows, avatar circles |
% | Parent container's dimension | Column widths (50%), images (width: 100%) |
rem | Root <html> font-size (16px) | Fonts, paddings, margins (Accessible & predictable) |
em | Element's own font-size | Buttons that scale padding with text |
vw | Total browser window width | Responsive full-width ribbons, fluid text |
vh | Total browser window height | Full-screen hero banners (min-height: 100vh) |
Common Beginner Mistakes (Do's and Don'ts)
| What Beginners Do (Don't ā) | What You Should Do Instead (Do ā ) | Why? |
|---|---|---|
Using em for all font sizes across nested lists. | Use rem for body text and headings. | Deeply nested em creates an unintended snowball multiplication effect. |
Setting height: 100% expecting full screen. | Use min-height: 100vh;. | height: 100% only works if every ancestor (including body and html) has an explicit height declared. |
Forgetting that 1rem = 16px by default. | Use simple math: 2rem = 32px, 1.5rem = 24px. | Remembering the 16px base makes mental conversion effortless. |
Quick Revision Summary
- Relative units adapt dynamically to parent elements or screen dimensions.
- Percentages (
%) scale relative to the parent container's width. remscales relative to the root<html>font size (default16px).emscales relative to the element's local font-size.100vwequals the entire browser screen width;100vhequals the entire screen height.- Modern web best practice: Use
remfor typography and spacing, and%/vw/vhfor layouts.
Practice Quiz
Test your understanding with these multiple-choice questions:
1. What does the "r" in the CSS unit rem stand for?
A. Relative B. Ratio C. Root D. Responsive Answer: C Explanation: rem stands for Root EM. It is calculated relative to the font-size of the root <html> element.
2. If the browser's default root font size is 16px, what is the computed size of margin-bottom: 2rem;?
A. 18px B. 24px C. 32px D. 64px Answer: C Explanation: 2rem is calculated as 2 * 16px = 32px.
3. Which relative unit is ideal for creating a hero banner that fills 100% of the user's visible vertical screen?
A. 100% B. 100px C. 100vw D. 100vh Answer: D Explanation: 100vh equals 100% of the viewport height, ensuring the container fills the entire visible window height.
4. Why is rem generally preferred over em for styling headings and paragraphs?
A. rem renders faster in JavaScript B. rem avoids the compound multiplication (snowball) issue that happens with nested em elements C. em is deprecated in HTML5 D. rem works only on computers Answer: B Explanation: Because rem always references the single root <html> element, it does not compound or multiply unpredictably when elements are nested inside one another.
5. If a parent container has a width of 800px, what is the actual rendered width of a child with width: 75%;?
A. 750px B. 600px C. 550px D. 700px Answer: B Explanation: 75% of 800px is calculated as (75 / 100) * 800 = 600px.
Practice Challenge (Try It Yourself)
- 1Open your code editor and create
relative-units.html. - 2Build a full-screen landing page layout using
vh,rem, and%:
- 1Open the file in your browser and resize the window height and width. Notice how the hero section stays anchored to the exact screen height! šÆ
Responsive Units & The calc() Function
Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
Related Lessons
| Previous Lesson | Next Lesson |
|---|---|
| Absolute Units: Pixels (px), Points (pt) | Responsive Units & The calc() Function |
Practice Quiz
Test your understanding of this lesson with 5 questions. Each question has one correct answer.