Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Using Relative Units in CSS for Responsive Text
📖 Scenario: You are creating a simple webpage that needs to have text that adjusts size based on the user's device or browser settings. This helps make your page easier to read on phones, tablets, and desktops.
🎯 Goal: Build a webpage with a heading and paragraph where the font sizes use relative units rem and em so the text scales nicely on different screen sizes.
📋 What You'll Learn
Use rem units for the heading font size
Use em units for the paragraph font size
Set the root font size in the html selector
Include a <header> with a heading and a <main> with a paragraph
Make sure the CSS uses relative units only for font sizes
💡 Why This Matters
🌍 Real World
Websites need to be readable on many devices. Using relative units like <code>rem</code> and <code>em</code> helps text scale properly for phones, tablets, and desktops.
💼 Career
Front-end developers use relative units to create accessible and responsive designs that adapt to user preferences and device sizes.
Progress0 / 4 steps
1
Create the HTML structure
Write the HTML skeleton with a <header> containing an <h1> with the text "Welcome to My Site" and a <main> containing a <p> with the text "This text will resize based on your device."
CSS
Hint
Remember to include the <header> and <main> tags with the exact text inside.
2
Set the root font size using html selector
Add a CSS rule for the html selector that sets the font size to 16px. This will be the base size for relative units.
CSS
Hint
Use the html selector and set font-size: 16px; inside the <style> tag.
3
Use rem for the heading font size
Add a CSS rule for the header h1 selector that sets the font size to 2rem. This means the heading will be twice the root font size.
CSS
Hint
Use the selector header h1 and set font-size: 2rem; inside the CSS.
4
Use em for the paragraph font size
Add a CSS rule for the main p selector that sets the font size to 1.25em. This means the paragraph text size is 1.25 times the size of its parent element's font size.
CSS
Hint
Use the selector main p and set font-size: 1.25em; inside the CSS.
Practice
(1/5)
1. Which CSS unit is relative to the root element's font size?
easy
A. em
B. rem
C. px
D. vw
Solution
Step 1: Understand root-relative units
The rem unit always refers to the font size set on the root <html> element.
Step 2: Compare with other units
em is relative to the parent element's font size, px is fixed pixels, and vw is relative to viewport width.
Final Answer:
rem -> Option B
Quick Check:
Root font size = rem [OK]
Hint: Root font size uses rem, not em or px [OK]
Common Mistakes:
Confusing em with rem
Thinking px is relative
Mixing viewport units with font units
2. Which of the following is the correct syntax to set a font size to 2 times the parent element's font size?
easy
A. font-size: 2vw;
B. font-size: 2rem;
C. font-size: 2px;
D. font-size: 2em;
Solution
Step 1: Identify the unit for parent-relative size
em units scale relative to the parent element's font size.
Step 2: Check other options
rem is root-relative, px is fixed, and vw is viewport width relative.
Final Answer:
font-size: 2em; -> Option D
Quick Check:
Parent-relative font size = em [OK]
Hint: Use em for parent-relative sizes, rem for root-relative [OK]
Common Mistakes:
Using rem instead of em for parent size
Using px which is fixed size
Confusing vw with font size units
3. Given the CSS below, what will be the width of the <div> if the viewport width is 1000px?
div {
width: 50vw;
}
medium
A. Depends on parent width
B. 50px
C. 500px
D. 1000px
Solution
Step 1: Understand vw unit
1vw equals 1% of the viewport width. So 50vw is 50% of viewport width.
Step 2: Calculate width
Viewport width is 1000px, so 50vw = 50% of 1000px = 500px.
Final Answer:
500px -> Option C
Quick Check:
50vw = 50% viewport width = 500px [OK]
Hint: vw is % of viewport width, multiply by viewport size [OK]
Common Mistakes:
Thinking vw depends on parent width
Confusing vw with px
Calculating 50vw as 50px
4. What is wrong with this CSS if the goal is to make the font size 1.5 times the parent font size?
p {
font-size: 1.5rem;
}
medium
A. rem is root-relative, not parent-relative
B. 1.5rem is invalid syntax
C. font-size cannot use rem units
D. Should use px instead of rem
Solution
Step 1: Identify unit behavior
rem units are relative to the root font size, not the parent element.
Step 2: Match goal with unit
To scale relative to the parent font size, em should be used instead of rem.
Final Answer:
rem is root-relative, not parent-relative -> Option A
Quick Check:
Parent-relative size needs em, not rem [OK]
Hint: Use em for parent-relative font size, not rem [OK]
Common Mistakes:
Thinking rem scales with parent
Believing 1.5rem is invalid syntax
Using px for scalable font sizes
5. You want a button width to be 30% of the viewport width but never smaller than 200px. Which CSS snippet correctly uses relative units and a minimum width?
hard
A. button { width: 30vw; min-width: 200px; }
B. button { width: 30%; min-width: 200vw; }
C. button { width: 30rem; min-width: 200px; }
D. button { width: 30em; min-width: 200%; }
Solution
Step 1: Understand width units
30vw means 30% of viewport width, which matches the requirement.
Step 2: Check minimum width
min-width: 200px ensures the button never shrinks below 200 pixels.
Step 3: Verify other options
Other options fail: 30rem is root font-relative; 30% with min-width: 200vw uses parent-relative width and huge min-width; 30em with min-width: 200% uses font-relative and parent-relative units incorrectly.
Final Answer:
button { width: 30vw; min-width: 200px; } -> Option A
Quick Check:
30vw + min-width: 200px [OK]
Hint: Use vw for viewport %, min-width in px for fixed minimum [OK]