CSS units affect responsiveness and accessibility. Use the right unit for the job instead of defaulting to pixels everywhere.
Unit groups
- Absolute:
px(device-independent CSS pixels). - Relative to font:
em,rem. - Viewport units:
vw,vh,dvh, etc. - Percentage: relative to containing context.
Production checklist
- Use
remfor scalable typography spacing systems. - Avoid fragile full-height mobile layouts with old
100vhassumptions. - Test zoom and system font-size changes.
Important interview questions and answers
- Q:
emvsrem?
A:emis relative to parent/current font-size;remis relative to root font-size. - Q: Why not only px?
A: Relative units adapt better to accessibility scaling and varied contexts. - Q: Mobile viewport gotcha?
A: Browser UI chrome can distort classicvhbehavior on mobile.
Pitfall: Check cascade order—author stylesheet loses to inline styles and !important surprises.