What if your website could magically fit every screen perfectly without you lifting a finger?
Why Relative units in CSS? - Purpose & Use Cases
Start learning this pattern below
Jump into concepts and practice - no test required
Imagine you are designing a website and you set all font sizes and spacing using fixed pixel values like 16px, 20px, or 30px.
At first, it looks fine on your screen, but when you view it on a phone, tablet, or a bigger monitor, the text and layout either look too small or too big.
Using fixed sizes means your site doesn't adjust well to different screen sizes or user preferences.
Users with poor eyesight might struggle because text can't scale properly.
It's also a lot of work to change every pixel value manually for different devices.
Relative units like em, rem, %, and vw let your design adapt automatically.
They scale based on the user's settings or the size of the screen, making your site flexible and easier to maintain.
font-size: 16px; margin: 20px;
font-size: 1rem; margin: 1.25rem;
Relative units enable your website to look good and be readable on any device or screen size without extra work.
Think about reading a news article on your phone versus a desktop. With relative units, the text adjusts so it's comfortable to read on both devices.
Fixed sizes don't adapt well to different screens or user needs.
Relative units make designs flexible and accessible.
They save time by reducing manual adjustments for different devices.
Practice
Solution
Step 1: Understand root-relative units
Theremunit always refers to the font size set on the root<html>element.Step 2: Compare with other units
emis relative to the parent element's font size,pxis fixed pixels, andvwis relative to viewport width.Final Answer:
rem -> Option BQuick Check:
Root font size = rem [OK]
- Confusing em with rem
- Thinking px is relative
- Mixing viewport units with font units
Solution
Step 1: Identify the unit for parent-relative size
emunits scale relative to the parent element's font size.Step 2: Check other options
remis root-relative,pxis fixed, andvwis viewport width relative.Final Answer:
font-size: 2em; -> Option DQuick Check:
Parent-relative font size = em [OK]
- Using rem instead of em for parent size
- Using px which is fixed size
- Confusing vw with font size units
<div> if the viewport width is 1000px?div {
width: 50vw;
}Solution
Step 1: Understand vw unit
1vwequals 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 CQuick Check:
50vw = 50% viewport width = 500px [OK]
- Thinking vw depends on parent width
- Confusing vw with px
- Calculating 50vw as 50px
p {
font-size: 1.5rem;
}Solution
Step 1: Identify unit behavior
remunits 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,emshould be used instead ofrem.Final Answer:
rem is root-relative, not parent-relative -> Option AQuick Check:
Parent-relative size needs em, not rem [OK]
- Thinking rem scales with parent
- Believing 1.5rem is invalid syntax
- Using px for scalable font sizes
Solution
Step 1: Understand width units
30vwmeans 30% of viewport width, which matches the requirement.Step 2: Check minimum width
min-width: 200pxensures the button never shrinks below 200 pixels.Step 3: Verify other options
Other options fail:30remis root font-relative;30% with min-width: 200vwuses 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 AQuick Check:
30vw + min-width: 200px [OK]
- Using % for viewport width instead of vw
- Setting min-width in vw or % incorrectly
- Confusing rem/em with viewport units
