CSS Responsive & Modern Features

Viewport Units

Master viewport units (vw, vh, vmin, vmax) to create truly responsive designs that adapt to any screen size without complex media queries.

What are vh and vw in CSS?

vw and vh are viewport units: 1vw is 1% of the viewport width and 1vh is 1% of its height. On mobile, 100vh can be taller than the visible screen because of the browser's address bar, so use 100dvh (dynamic) or 100svh (small) for full-screen sections.

.hero  { min-height: 100svh; }      /* fits the visible screen */
.title { font-size: clamp(2rem, 5vw, 4rem); }

Interactive Viewport Units Demo

Code Editor

<section class="hero">
  <h1 class="title">Resize the preview</h1>
  <p class="size"></p>
</section>

Preview

1. Unit Types

Understanding Viewport Units

Learn the four core viewport units and how they relate to your browser window size.

VW

Viewport Width

.element {
  width: 50vw; /* 50% of viewport width */
  font-size: 4vw;
}

1vw = 1% of the viewport width. Perfect for horizontal scaling and responsive typography.

VH

Viewport Height

.hero {
  height: 100vh; /* Full screen height */
  min-height: 50vh;
}

1vh = 1% of the viewport height. Ideal for full-screen sections and vertical layouts.

MIN

Viewport Minimum

.square {
  width: 50vmin; /* 50% of smaller dimension */
  height: 50vmin;
}

1vmin = 1% of the smaller viewport dimension. Great for creating consistent square elements.

MAX

Viewport Maximum

.billboard {
  font-size: 8vmax; /* Scales with larger dimension */
  padding: 2vmax;
}

1vmax = 1% of the larger viewport dimension. Perfect for dramatic scaling effects.

Quick Reference

vw: Viewport width percentage

vh: Viewport height percentage

vmin: Smaller dimension percentage

vmax: Larger dimension percentage

Modern Units

Dynamic Viewport Units

New viewport units that handle mobile browsers with dynamic UI elements more effectively.

DVH

Dynamic VH

.mobile-hero {
  height: 100vh;
  height: 100dvh; /* Fallback */
}

Adapts to mobile browser UI changes automatically

SVH

Small VH

.guaranteed-visible {
  height: 100svh;
  /* Always visible */
}

Uses the smallest possible viewport (UI visible)

LVH

Large VH

.full-screen {
  height: 100lvh;
  /* Maximum space */
}

Uses the largest possible viewport (UI hidden)

Mobile Browser Considerations

Mobile browsers often show/hide UI elements (address bar, tabs) which changes the viewport height. The new dynamic units handle this automatically.

dvh: Dynamically adjusts
svh: UI always visible
lvh: UI hidden state
2. Responsive Design

Responsive Typography & Layouts

Create adaptive designs that scale smoothly across all screen sizes without media queries.

Fluid Typography

Typography with Viewport Units

/* Basic viewport-based typography */
h1 {
  font-size: 5vw; /* Simple but can be too extreme */
}

/* Better approach with clamp() */
h1 {
  font-size: clamp(2rem, 5vw, 4rem);
  /* min: 2rem, preferred: 5vw, max: 4rem */
}

/* Responsive paragraph text */
p {
  font-size: clamp(1rem, 1.5vw, 1.25rem);
  line-height: clamp(1.4, 1.5vw + 1rem, 1.7);
}

/* Scalable spacing */
.section {
  padding: clamp(2rem, 5vw, 6rem) clamp(1rem, 3vw, 3rem);
  margin-bottom: clamp(3rem, 8vw, 8rem);
}

Use clamp() with viewport units for typography that scales smoothly across all screen sizes.

Typography Scale System

/* Modular scale with viewport units */
:root {
  --base-size: clamp(1rem, 1.2vw, 1.125rem);
  --scale-factor: 1.25;
}

.text-sm {
  font-size: calc(var(--base-size) / var(--scale-factor));
}

.text-base {
  font-size: var(--base-size);
}

.text-lg {
  font-size: calc(var(--base-size) * var(--scale-factor));
}

.text-xl {
  font-size: calc(var(--base-size) * var(--scale-factor) * var(--scale-factor));
}

Create a responsive typography system with consistent scaling

Accessible Typography

/* Respect user preferences */
@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* Minimum readable sizes */
p {
  font-size: max(1rem, 1.2vw);
  /* Never smaller than 1rem */
}

/* High contrast mode */
@media (prefers-contrast: high) {
  .text {
    font-weight: 600;
    text-shadow: none;
  }
}

Ensure accessibility while using responsive typography

3. Practical Uses

Layout Applications

Learn how to use viewport units in real-world layouts and components.

Common Layout Patterns

Full-Height Layouts

/* Full viewport hero section */
.hero {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Account for mobile browsers */
.hero-mobile {
  height: 100vh;
  height: 100dvh; /* Dynamic viewport height */
}

/* Sticky header layout */
.layout {
  height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.main {
  overflow-y: auto;
  height: calc(100vh - var(--header-height) - var(--footer-height));
}

Create layouts that perfectly fit the viewport height.

Typography Scale

/* Modular scale with viewport units */ :root {
  --base-size: clamp(1rem, 1.2vw, 1.125rem);
  --scale-factor: 1.25;
}

.text-sm {
  font-size: calc(var(--base-size) / var(--scale-factor));
}

.text-base {
  font-size: var(--base-size);
}

.text-lg {
  font-size: calc(var(--base-size) * var(--scale-factor));
}

.text-xl {
  font-size: calc(var(--base-size) * var(--scale-factor) * var(--scale-factor));
}

Create a responsive typography system with consistent scaling

Accessible Typography

/* Respect user preferences */ @media (prefers-reduced-motion: reduce) {
  * { transition: none !important; }
}

/* Minimum readable sizes */ p {
  font-size: max(1rem, 1.2vw);
  /* Never smaller than 1rem */
}

/* High contrast mode */ @media (prefers-contrast: high) {
  .text {
    font-weight: 600;
    text-shadow: none;
  }
}

/* Large text preference */ @media (prefers-reduced-motion: no-preference) {
  .smooth-text {
    transition: font-size 0.2s ease;
  }
}

Ensure accessibility while using responsive typography

Layout Applications

Full-Height Layouts

/* Full viewport hero section */ .hero {
  height: 100vh;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* Account for mobile browsers */ .hero-mobile {
  height: 100vh;
  height: 100dvh; /* Dynamic viewport height */
}

/* Sticky header layout */ .layout {
  height: 100vh;
  display: grid;
  grid-template-rows: auto 1fr auto;
}

.header { /* Fixed height */ }
.main {
  overflow-y: auto;
  height: calc(100vh - var(--header-height) - var(--footer-height));
}
.footer { /* Fixed height */ }

Create layouts that perfectly fit the viewport height.

Responsive Sidebars

/* Proportional sidebar */
.layout {
  display: flex;
  height: 100vh;
}

.sidebar {
  width: clamp(200px, 25vw, 300px);
  background: #f8f9fa;
}

.main {
  flex: 1;
  padding: clamp(1rem, 3vw, 2rem);
}

/* Collapsible at small screens */
@media (max-width: 768px) {
  .sidebar {
    width: 100vw;
    height: auto;
  }
}

Create responsive sidebars that adapt to viewport size

Modal and Overlay Sizing

/* Responsive modal */
.modal {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: clamp(300px, 90vw, 600px);
  max-height: 90vh;
  overflow-y: auto;
}

/* Full-screen overlay */
.overlay {
  position: fixed;
  top: 0;
  left: 0;
  width: 100vw;
  height: 100vh;
  background: rgba(0, 0, 0, 0.5);
}

Use viewport units for overlays and modal components

4. Best Practices

Browser Considerations & Guidelines

Essential guidelines for using viewport units effectively and accessibly.

Best Practices

  • ✓Use clamp() to prevent text from becoming too small or large
  • ✓Consider mobile browsers with dynamic UI elements
  • ✓Test across different devices and orientations
  • ✓Provide fallbacks for older browsers
  • ✓Use CSS custom properties for maintainable code
  • ✓Consider accessibility and readability at all sizes

Common Pitfalls

  • ⚠Avoid pure viewport units for small text (use clamp)
  • ⚠Be careful with 100vh on mobile browsers
  • ⚠Don't use for critical UI elements without fallbacks
  • ⚠Test print stylesheets (viewport units may not work)
  • ⚠Consider users who zoom content
  • ⚠Account for iOS Safari's changing viewport

Perfect Use Cases

  • • Full-height hero sections (100vh/dvh)
  • • Responsive typography with clamp()
  • • Proportional layout components
  • • Modal and overlay sizing
  • • Scalable spacing and margins
  • • Square elements (using vmin)
  • • Fluid grid systems
  • • Dynamic content scaling

Browser Support Tips

/* Progressive enhancement */
.hero {
  height: 600px; /* Fallback */
  height: 100vh; /* Modern browsers */
  height: 100dvh; /* Latest browsers */
}

/* Feature detection */
@supports (height: 100dvh) {
  .hero { height: 100dvh; }
}

/* Safe defaults */
.text {
  font-size: 1.2rem; /* Fallback */
  font-size: clamp(1rem, 2vw, 1.5rem);
}

Always provide fallbacks and use progressive enhancement

CSS Viewport Units — step by step

1 / 4
1

vw and vh

1vw equals 1% of the viewport width. 1vh equals 1% of the viewport height. Use these for full-screen sections: height: 100vh fills the full screen.

1 of 4
💡Use dvh Instead of vh on Mobile
100vh on mobile includes the hidden browser address bar, causing content to overflow. Use 100dvh (dynamic viewport height) instead — it updates in real-time as the browser chrome appears and disappears.

🎯 Quick Check

Which viewport unit updates dynamically as the mobile browser bar shows or hides?

CSS Viewport Units: summary

Key points

  • svh is the height with browser UI visible, lvh the height with it hidden, and dvh updates as the UI shows and hides.
  • vmin and vmax use the smaller or larger of the two viewport dimensions.
  • 100vw includes the vertical scrollbar width on desktop, which can cause horizontal scrolling. Prefer 100%.
  • Wrap viewport-based font sizes in clamp() so text never becomes tiny or huge.

Frequently asked questions

›Why is 100vh too tall on mobile?

Mobile browsers calculate vh as if the address bar were hidden. Use 100svh or 100dvh instead.

›What is the difference between vw and %?

% is relative to the parent element, while vw is always relative to the viewport width, whatever the parent's size.