CSS Units are used to define sizes, spacing, widths, heights, typography, positioning, and layouts in CSS.
CSS units help control:
CSS units are mainly divided into two categories:
Absolute units have fixed sizes.
They do not change based on screen size or parent elements.
| Unit | Description |
|---|---|
| px | Pixels |
| cm | Centimeters |
| mm | Millimeters |
| in | Inches |
| pt | Points |
px is the most commonly used fixed-size unit.
Relative units depend on parent size, root size, viewport, or content size.
| Unit | Relative To |
|---|---|
| % | Parent size |
| em | Parent font size |
| rem | Root font size |
| vw | Viewport width |
| vh | Viewport height |
| vmin | Smaller viewport side |
| vmax | Larger viewport side |
| ch | Width of "0" character |
The element takes 50% width of its parent.
Depends on the parent font size.
Depends on the root:
[code theme="dark"] html [/code]font size.
The section becomes full screen height.
Text size scales based on viewport width.
Modern CSS introduced new mobile-friendly viewport units:
| Unit | Description |
|---|---|
| svh | Small viewport height |
| lvh | Large viewport height |
| dvh | Dynamic viewport height |
Useful for mobile browsers with dynamic address bars.
| Unit | Responsive | Best Use |
|---|---|---|
| px | No | Fixed UI elements |
| rem | Yes | Typography |
| % | Yes | Layout sizing |
| vw / vh | Yes | Fullscreen layouts |
Most CSS units are supported across all modern browsers.
Older browsers may not support newer dynamic viewport units fully.
CSS units are one of the most important foundations of responsive and modern frontend development.