Lesson 9 of 50
cssUnits of Measurement
8 mins
Course progress18%
What you'll learn
Keep these ideas in mind as you work through the lesson.
Lesson 9 of 50
css8 mins
Keep these ideas in mind as you work through the lesson.
CSS offers a palette of units. Choosing the right one determines how responsive and maintainable your layout will be.
px – pixel (device‑independent). Most designers think in pixels.pt, cm, mm, in – print‑oriented; useful only for print stylesheets.| Unit | Relative to | When to use |
|---|---|---|
em | Parent element’s font-size. | Component‑level scaling (e.g., button padding that grows with its container’s font size). |
rem | Root (<html>) font-size. | Global spacing, typographic rhythm, whole‑site scaling. |
% | Parent’s width/height (depending on the property). | Fluid widths, heights, or line‑height percentages. |
| Unit | Definition |
|---|---|
1vw | 1 % of the viewport width. |
1vh | 1 % of the viewport height. |
vmin | Smaller of vw or vh. |
vmax | Larger of vw or vh. |
.hero {
height: 100vh; /* 100 % of viewport height */
display: flex;
align-items: center;
justify-content: center;
background: #222;
color: #fff;
}
Never mix px with em/rem on the same property. It creates a mental overhead and can lead to unexpected scaling. Stick to a single‑system per project (e.g., rem‑based spacing everywhere, vw for fluid typography).