PX vs REM vs EM vs VW vs VH
| Unit | Relative to | Best for | Avoid for |
|---|---|---|---|
| px | Device pixel | Borders, icons | Typography |
| rem | Root font-size | Text, spacing | Component isolation |
| em | Parent font-size | Components | Deep nesting |
| vw | Viewport width | Fluid layouts | Body text |
| vh | Viewport height | Full-height sections | Mobile UI chrome |
Recommended usage
- Use rem for typography and spacing
- Use em for component-level scaling
- Use px for fine details
- Use vw / vh for layout proportions
- Combine units with
clamp()for responsive text
What is px?
px is an absolute CSS unit.
- Does not scale with user font settings
- Ideal for borders, hairlines and icons
- Not recommended for typography
What is rem?
rem (root em) is relative to the root element’s font-size.
- Scales consistently across the document
- Respects accessibility settings
- Recommended for typography and spacing
Default browser root font-size is 16px.
What is em?
em is relative to the font-size of the current element.
- Useful for component-scoped scaling
- Scales children together
- Can compound when nested
What are vw and vh?
Viewport-relative units.
1vw= 1% of viewport width1vh= 1% of viewport height- Useful for fluid layouts and hero sections
Avoid using viewport units for small text sizes.
Common mistakes with CSS units
rembehaves differently inside media queriesemcompounds when deeply nestedvhchanges on mobile when browser UI appearsvwtext can become unreadable on large screens
Example CSS usage
html {
font-size: 16px;
}
h1 {
font-size: 2rem;
}
.container {
padding: 4vw;
}
How this CSS unit converter works
- rem is calculated from the root (
html) font-size - em is calculated from the parent element’s font-size
- vw and vh are calculated from the current viewport size
- All conversions run through px for consistency