Complete guide to all CSS measurement units with examples
| Unit | Type | Relative To | Use Case | Example |
|---|---|---|---|---|
| px | Absolute | Device pixel | Fixed sizes, borders | border: 1px solid #000; |
| % | Relative | Parent element | Layout widths, heights | width: 80%; |
| em | Relative | Parent font-size | Typography scaling | font-size: 1.2em; |
| rem | Relative | Root element font-size | Consistent typography | padding: 1.5rem; |
| vw | Viewport | Viewport width | Full-width elements | width: 100vw; |
| vh | Viewport | Viewport height | Full-height sections | height: 100vh; |
| vmin | Viewport | Smaller viewport dimension | Mobile-responsive text | font-size: 4vmin; |
| vmax | Viewport | Larger viewport dimension | Large screen optimization | padding: 2vmax; |
| fr | Flexible | Available space in grid | CSS Grid layouts | grid-template-columns: 1fr 2fr; |
Example: An element with height: 50vh will always be 50% of the visible screen height, regardless of scrolling.
Example: If html font-size is 16px, then 1rem = 16px everywhere, but 1em could be different in each context.
width: min(1000px, 90vw) creates a responsive container that's never wider than 1000px or 90% of the viewport.