CSS uses different types of units to define sizes, spacing, fonts, and layouts. These units can be absolute (fixed) or relative (depending on the parent or viewport).
.box { width: 200px; }
.box { width: 50%; }
.parent { font-size: 20px; }
.child { font-size: 2em; } /* 40px */
html { font-size: 16px; }
.box { padding: 2rem; } /* 32px */
.hero { font-size: 5vw; }
.section { height: 100vh; }
| Unit | Type | Description | Example |
|---|---|---|---|
px |
Absolute | Fixed size, does not scale | width: 100px; |
% |
Relative | Relative to parent size | width: 50%; |
em |
Relative | Relative to parent font size | font-size: 2em; |
rem |
Relative | Relative to root (html) font size |
padding: 1.5rem; |
vw |
Relative | 1% of viewport width | font-size: 5vw; |
vh |
Relative | 1% of viewport height | height: 100vh; |