HTML & CSS
/Intermediate
CSS Units (px, rem, em, %, vh, vw)
Definition
CSS provides various units of measurement to express length. They are divided into absolute units (like `px`) and relative units (like `%` and `rem`).
Explain Like I'm New
`px` is absolute: a 16px font is always exactly 16 pixels regardless of screen size. `%`, `vw`, and `rem` are relative. If you say `width: 50%`, it means half of whatever the parent container's size is. This is the absolute foundation of making websites mobile-friendly.
Real World Example
Setting a hero banner to `height: 100vh;` means it will always perfectly fill 100% of the Viewer's screen Height, whether they are on a massive TV or a tiny iPhone.
Common Use Cases
- •Responsive typography
- •Fluid layouts
Interactive Example
Interview Questions
basic
- What is the difference between `vh` and `vw`?
intermediate
- Why is `rem` preferred over `px` for font sizes?