CSS Units & Sizing (px, rem, em, %, vh, dvh)
Master CSS length units: Absolute (px) vs Relative (rem, em, %) and Viewport units (vh, vw, dvh, svh). Learn when to use each unit for responsive accessibility.
CSS Units In-Depth Overview & Use Cases
Selecting the appropriate CSS unit for dimensions, font sizes, and layout spacing is essential for responsive, accessible web design. Using fixed pixel (px) values everywhere breaks layout scaling when users change default browser font sizes or view sites on mobile devices.
CSS divides measurement units into Absolute units (fixed physical lengths like px, pt) and Relative units (calculated dynamically relative to root font size, parent dimensions, or screen viewport bounds). Modern CSS also introduces dynamic mobile viewport units (dvh, svh) that adapt smoothly as browser address bars expand and collapse.
Key Real-World Use Cases
Using rem for font sizes so text scales proportionally when users increase browser zoom or system font size.
Using em for button padding so internal icon/text padding scales automatically when button font-size changes.
Using 100dvh (Dynamic Viewport Height) to prevent unwanted mobile browser scrollbars caused by collapsing URL address bars.
Using px for hair-line borders (1px) and subtle box-shadow offsets that should remain fixed at high DPI resolution.
Absolute Units (px) vs Relative Units (rem, em, %)
Absolute units are anchored to fixed physical measurements, whereas relative units compute their value based on surrounding context or font size definitions.
| Unit | Type | Relative To | Best Used For |
|---|---|---|---|
| px | Absolute | 1 physical pixel (screen pixel ratio) | Borders (1px), subtle shadow offsets, micro-dividers |
| rem | Relative | Root element font size (usually 16px) | Global font sizes, layout margins, padding, component widths |
| em | Relative | Parent / Current element font size | Component-scoped padding, icon sizes bound to parent text |
| % | Relative | Parent container dimension | Fluid layout column widths, image max-width bounds |
Standard Viewport Units (vh, vw, vmin, vmax)
Viewport units are calculated as percentages of the visible browser window size:
- 1vw: Equal to 1% of total viewport width.
- 1vh: Equal to 1% of total viewport height.
- 1vmin: Equal to 1% of the smaller viewport dimension (width or height).
- 1vmax: Equal to 1% of the larger viewport dimension (width or height).
Modern Mobile Viewport Units (dvh, svh, lvh)
On mobile browsers (Safari iOS, Chrome Mobile), standard 100vh includes the height beneath expandable browser address bars, causing content clipping.
Modern CSS introduced dynamic viewport units to solve mobile browser UI changes:
| Unit | Full Name | Behavior on Mobile Devices |
|---|---|---|
| svh / svw | Small Viewport Height/Width | Calculated when mobile browser address bar is EXPANDED (smallest visible height). |
| lvh / lvw | Large Viewport Height/Width | Calculated when mobile browser address bar is COLLAPSED (largest visible height). |
| dvh / dvw | Dynamic Viewport Height/Width | Dynamically adapts in real time as the browser address bar expands or shrinks! |