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.
.hero { min-height: 80vh; } h1 { font-size: clamp(2rem, 6vmin, 4rem); }
Try it
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
<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) andvb(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
100vwincludes the scrollbar. On systems with classic scrollbars,width: 100vwis wider than the visible page, which creates a horizontal scrollbar. Usewidth: 100%or let a block element fill the width by itself.100vhon phones. The classic units use the largest viewport, so on mobile browsers whose toolbars slide away,100vhcan be taller than the visible area. Seedvh,svhandlvh.- Text size. Pure
vwfont sizes ignore user preferences and can become unreadably small or huge. Combine withreminsideclamp(), for exampleclamp(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
cqwdoes. - Steady layouts.
scrollbar-gutter: stableon 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.
Related
dvh,svh,lvhโ mobile-aware viewport unitscqwand container units โ relative to a containerclamp()โ limits fluid sizes%โ relative to the parent- Lesson: Viewport Units