Course contentsShow
← Web Development
Lesson 156 of 2,9823. CSS FundamentalsFree lesson

Viewport Units: vw, vh, vmin, vmax

Master viewport-relative units for responsive sizing based on browser window dimensions.

Viewport Units: vw, vh, vmin, vmax

What you'll learn: How to size elements based on the browser window itself, creating truly responsive layouts that adapt to any screen size.

What Are Viewport Units?

Viewport units are CSS measurements that are relative to the size of the browser window (the viewport). Unlike em and rem which depend on font sizes, or px which are fixed, viewport units automatically scale with the window dimensions.

Think of viewport units like percentages of your screen: if the browser window changes size, these units adjust instantly.

The Four Viewport Units

vw (viewport width): 1vw equals 1% of the viewport's width. If your browser is 1000px wide, 10vw = 100px. Resize the window? The size updates automatically.

vh (viewport height): 1vh equals 1% of the viewport's height. Perfect for creating full-screen sections or elements that scale with window height.

vmin (viewport minimum): 1vmin equals 1% of whichever dimension is smaller (width or height). On a phone in portrait mode, this follows the width. Rotate to landscape, and it follows the height.

vmax (viewport maximum): 1vmax equals 1% of whichever dimension is larger. The opposite of vmin.

Practical Example

.hero-section {
  width: 100vw;  /* Full width of viewport */
  height: 100vh; /* Full height of viewport */
}

.responsive-heading {
  font-size: 5vw; /* Scales with viewport width */
}

.square {
  width: 50vmin;  /* Always 50% of the smaller dimension */
  height: 50vmin; /* Creates a perfect square on any screen */
}

Key Takeaway: Viewport units (vw, vh, vmin, vmax) let you size elements as percentages of the browser window itself, creating flexible designs that respond instantly to screen size changes without media queries.