DevPrep
  • Interview Prep
  • Projects
  • Resources
  • Pricing
  • About Us
Submit Question
DevPrep
  • Pricing
  • About Us
Submit Question
← From Beginner to Expert in Fundamentals of Frontend
  • The Box Model
  • Specificity & The Cascade
  • CSS Units Deep Dive
  • Stacking Contexts & Z-Index
  • Flexbox Mastery
  • CSS Grid Mastery
  • Centering, Subgrid & Container Queries
  • CSS Custom Properties & Variables
  • Selectors: :has(), Pseudo-classes & Pseudo-elements
  • Responsive Functions: calc(), clamp(), min(), max()
  • Media Queries & Mobile-First Design
  • Transitions & Animations
  • Transforms, Filters & Clipping
  • CSS Methodologies: BEM, CSS-in-JS & Tailwind
  • Typography, Color Spaces & Dark Mode
  • CSS Performance & Optimization
  • CSS Interview Questions — Part 1
  • CSS Interview Questions — Part 2
← From Beginner to Expert in Fundamentals of Frontend

CSS Units Deep Dive

15 min read
pxemremvwvhdvhfrchViewport UnitsAccessibility

Absolute vs. Relative Units

Choosing the right CSS unit is critical for responsive design and accessibility. Using px everywhere is the #1 mistake beginners make.

Complete Unit Reference

UnitTypeRelative ToBest For
pxAbsoluteScreen pixelBorders, shadows, fine details
emRelativeParent font-sizeComponent-scoped sizing
remRelativeRoot font-size (html)Font sizes, spacing (accessibility ✅)
%RelativeParent elementWidths, fluid layouts
vwRelativeViewport widthFull-width sections
vhRelativeViewport heightHero sections (⚠️ mobile issues)
dvhRelativeDynamic viewport heightMobile-safe full height
svhRelativeSmallest viewport heightConservative full height
lvhRelativeLargest viewport heightMaximum full height
vminRelativeSmaller of vw/vhResponsive squares
vmaxRelativeLarger of vw/vhFilling larger dimension
chRelativeWidth of "0" characterLimiting text width (65ch)
frRelativeFree space in gridGrid column/row sizing

Why rem for Font Sizes?

When users change their browser's default font size (for accessibility), px values don't scale. rem values do, because they're relative to the root font size.

This is a WCAG accessibility requirement. Using px for font sizes fails accessibility audits.

The Mobile vh Problem

On mobile browsers, 100vh includes the area behind the address bar, causing content to be hidden. Use 100dvh (dynamic viewport height) instead, which accounts for the browser chrome.

Practice

  • JavaScript
  • DSA
  • Machine Coding
  • System Design

Resources

  • Learning Tracks
  • Articles
  • Roadmaps
  • Compare Concepts
  • Glossary
  • Developer Tools
  • All Questions

Company

  • About
  • Pricing

Legal

  • Privacy Policy
  • Terms of Service
DevPrep

© 2026 DevPrep. All rights reserved.