Hack Frontend Community

What are vh, vw, vmin and vmax in CSS

Viewport Units: vh, vw, vmin, vmax

These CSS units are based on viewport dimensions. This is useful for creating adaptive, "fluid" layouts.


Unit Overview

UnitValueMeaning
1vh1% of window heightHeight in 1/100 of viewport height
1vw1% of window widthWidth in 1/100 of viewport width
1vmin1% of smaller value between vh or vwMinimum of width/height
1vmax1% of larger value between vh or vwMaximum of width/height

Example

.box {
  width: 50vmin;   /* will be 50% of smaller window side */
  height: 50vmax;  /* will be 50% of larger window side */
  background: lightcoral;
}

Such a block will adapt to screen orientation: when rotating the device, vmin/vmax change.

Application

  • vh and vw — for fullscreen sections, blocks, slides.
  • vmin — so blocks don't exceed the smaller side.
  • vmax — for creating elements that stretch along the larger side (e.g., in horizontal layout).

Important for Mobile

  • Mobile browsers change vh height when showing/hiding the address bar. To solve this problem:

Use CSS variables with JavaScript:

document.documentElement.style.setProperty('--vh', `${window.innerHeight * 0.01}px`);
.full-height {
  height: calc(var(--vh, 1vh) * 100);
}

Tip:

Use vmin/vmax for responsive fonts, spacing, and interface scaling, especially if you don't want to tie to screen width or height only.