D DevBrainBox

CSS Viewport Units

CSS

What are Viewport Units?

Viewport units are relative units in CSS that are based on the size of the browser window (viewport).

Viewport Units

UnitMeaningBased on…
vwViewport Width1vw = 1% of browser width
vhViewport Height1vh = 1% of browser height
vminMinimum of vw or vhSmaller side of viewport
vmaxMaximum of vw or vhLarger side of viewport

1. vw (Viewport Width)

h1 {
  font-size: 10vw;  /* 10% of browser width */
}

The text size increases or decreases with screen width.

2. vh (Viewport Height)

.hero {
  height: 100vh;  /* full screen height */
}

Common for full-screen sections like banners.

3. vmin and vmax

.box {
  width: 50vmin;  /* 50% of the smaller screen dimension */
  height: 50vmax; /* 50% of the larger screen dimension */
}

Useful when you want elements to scale uniformly on all devices.

Examples

Fullscreen Section

.section {
  width: 100vw;
  height: 100vh;
  background: lightblue;
}

Responsive Font

p {
  font-size: 2vw;  /* scales with screen width */
}
UnitDescription
vw1% of the viewport width
vh1% of the viewport height
vmin1% of the smaller side
vmax1% of the larger side

On this page