Skip to content
Tutorials Center
My learning

Topics

CSS Reference ยท Units and Values

The vw, vh, vmin and vmax units

Reference for the CSS viewport units vw, vh, vmin and vmax: how they relate to the browser window, the scrollbar trap, mobile caveats and examples.

Viewport units are lengths measured against the size of the viewport, the visible area of the page. 1vw is 1% of the viewport width and 1vh is 1% of its height. vmin is 1% of the smaller dimension and vmax is 1% of the larger one.

They ignore the parent element, which makes them good for things that relate to the screen itself: full-screen sections, fluid headlines and modals that must fit the window.

CSS
.hero {
  min-height: 80vh;
}

h1 {
  font-size: clamp(2rem, 6vmin, 4rem);
}

Try it

The four viewport units

Inside a live preview, the viewport is the preview frame, not your whole browser window. The vmax bar is always the longest and vmin the shortest, whichever way the frame is shaped.

Syntax

CSS
<length> = <number>vw | <number>vh | <number>vmin | <number>vmax
  • Reference: the initial containing block, i.e. the viewport (using its large size on mobile for these classic units).
  • Applies to: any property that accepts a <length>.
  • Animatable: yes, as part of a length.
  • Logical versions: vi (viewport inline size) and vb (viewport block size) follow the writing mode.

Values

Value Description
vw 1% of the viewport width.
vh 1% of the viewport height.
vmin 1% of the smaller of the viewport's width and height.
vmax 1% of the larger of the viewport's width and height.
vi, vb 1% of the viewport size on the inline or block axis.

Usage notes

  • 100vw includes the scrollbar. On systems with classic scrollbars, width: 100vw is wider than the visible page, which creates a horizontal scrollbar. Use width: 100% or let a block element fill the width by itself.
  • 100vh on phones. The classic units use the largest viewport, so on mobile browsers whose toolbars slide away, 100vh can be taller than the visible area. See dvh, svh and lvh.
  • Text size. Pure vw font sizes ignore user preferences and can become unreadably small or huge. Combine with rem inside clamp(), for example clamp(1rem, 0.8rem + 1vw, 2rem).
  • Zoom still works. Browser zoom changes the viewport's CSS size, so viewport units respond to it.
  • Prefer container units for components. A card should react to its own space, which is what cqw does.
  • Steady layouts. scrollbar-gutter: stable on the root reserves room for a scrollbar, so layouts do not shift when one appears.

Browser support

Supported in every browser; vi and vb are also widely supported in current browsers.