Relative units help your website look good on different screen sizes and devices by adjusting sizes based on something else.
Relative units in CSS
Start learning this pattern below
Jump into concepts and practice - no test required
property: valueunit;Relative units depend on something else, like the size of the parent or the root element.
Common relative units include em, rem, %, vw, and vh.
font-size: 1.5em;
margin: 10%;
width: 50vw;
padding: 2rem;
This example shows a box that is half the width of the browser window. The text sizes and spacing use relative units so they scale nicely if you resize the window or change your browser's font size.
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <title>Relative Units Example</title> <style> :root { font-size: 16px; } body { font-family: Arial, sans-serif; margin: 2rem; } .container { width: 50vw; padding: 1rem; border: 2px solid #4a90e2; background-color: #e6f0fa; } h1 { font-size: 2rem; margin-bottom: 1rem; } p { font-size: 1.2em; margin-bottom: 1rem; } button { font-size: 1rem; padding: 0.5em 1em; background-color: #4a90e2; color: white; border: none; border-radius: 0.3rem; cursor: pointer; } button:hover { background-color: #357abd; } </style> </head> <body> <main class="container" role="main"> <h1>Relative Units Demo</h1> <p>This box is 50% of the viewport width (50vw).</p> <p>The font sizes and spacing use <code>rem</code> and <code>em</code> units.</p> <button aria-label="Click me button">Click Me</button> </main> </body> </html>
em units are relative to the font size of the parent element.
rem units are relative to the root (html) font size, usually 16px by default.
Viewport units like vw and vh depend on the browser window size, useful for responsive design.
Relative units help your page adapt to different screen sizes and user settings.
Use em for sizes relative to the parent element's font size.
Use rem for sizes relative to the root font size, and vw/vh for viewport-based sizes.
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
