Skip to main content

Beginner-Friendly Topic

Take your time - it's perfectly normal to re-read this topic 2-3 times. Try the interactive code editor below to run code yourself. Use the Q&A section to check your understanding before moving on.You've got this!

Lesson 3 of 10

Responsive Units (vw, vh, rem, %)

Responsive units adapt to screen size, parent size, or user preferences. Using the right units is essential for layouts that work on every device without hardcoded pixel values.

10 minBy Priygop Editorial TeamUpdated 2026Peer Reviewed & Verified

Responsive Unit Guide

  • rem - Relative to root font-size. Best for fonts, spacing, and padding. Respects user font settings
  • % - Relative to parent. width: 80% = 80% of parent width. Essential for fluid layouts
  • vw - 1% of viewport width. 100vw = full screen width. Useful for full-width sections
  • vh - 1% of viewport height. 100vh = full screen. Use for hero sections. Caution: 100vh can be taller than visible area on mobile
  • dvh - Dynamic viewport height. Accounts for mobile browser chrome (address bar). Use dvh instead of vh for mobile
  • ch - Width of the '0' character. max-width: 65ch = ideal reading line length
  • Use rem for most things - font-size: 1rem, padding: 1.5rem, gap: 1rem. Consistent and accessible

Responsive Units Code

Responsive Units Code
/* Responsive spacing with rem */
.section { padding: 3rem 1rem; }
.card { padding: 1.5rem; margin-bottom: 1rem; }
.btn { padding: 0.75rem 1.5rem; font-size: 1rem; }

/* Full-screen hero (mobile-safe) */
.hero {
  min-height: 100dvh; /* Dynamic viewport height */
  /* dvh accounts for mobile browser address bar! */
}

/* Readable text width */
.article-body {
  max-width: 65ch; /* Optimal reading width */
  margin: 0 auto;
  padding: 0 1rem;
}

/* Fluid container */
.container {
  width: 90%;
  max-width: 1200px;
  margin: 0 auto;
}

/* Full-width breakout from container */
.full-bleed {
  width: 100vw;
  margin-left: calc(-50vw + 50%);
}

Tip

Use dvh (dynamic viewport height) instead of vh for any element that needs to be full-screen on mobile. The difference is subtle but critical: 100dvh adjusts when the mobile browser's address bar shows/hides, preventing content from being hidden behind it.

Diagram
Loading diagramโ€ฆ

Modern dynamic viewport units (dvh, svh) prevent layout jumps on mobile browser address bar toggle

Common Mistake

Using vw units for font sizes without clamping. font-size: 4vw looks good on desktop but becomes tiny on mobile (320px ร— 4% = 12.8px). Always use clamp() with vw: font-size: clamp(1rem, 3vw, 2rem). This provides a floor and ceiling for the scaling value.

Practice Task

Refactor a fixed-unit layout to responsive units: (1) Replace height: 600px on a hero with min-height: 100dvh, (2) Replace font-size: 48px on a heading with clamp(2rem, 5vw, 4rem), (3) Replace padding: 80px 40px with padding: clamp(2rem, 5vw, 5rem) clamp(1rem, 3vw, 3rem). Resize to compare.

Quick Quiz

Key Takeaways

  • Responsive units adapt to screen size, parent size, or user preferences.
  • rem - Relative to root font-size. Best for fonts, spacing, and padding. Respects user font settings
  • % - Relative to parent. width: 80% = 80% of parent width. Essential for fluid layouts
  • vw - 1% of viewport width. 100vw = full screen width. Useful for full-width sections

Hands-on Practice Tools for this Course

Test what you learn using these free in-browser developer utilities (no signup required).

All 40+ Tools

Topics in This Module

0/10 done