Viewport units are a percentage of the browser window (“viewport”) size.
| Unit | 1 unit is |
|---|---|
vw | 1% of the viewport width |
vh | 1% of the viewport height |
vmin | 1% of the smaller of the two |
vmax | 1% of the larger of the two |
So 100vw is the full window width and 50vh is half its height.
.hero {
min-height: 100vh; /* at least as tall as the screen */
display: grid;
place-items: center;
}
That is a full-screen intro section in three lines.
Seeing them in action
The Result panel is its own small window, so viewport units here refer to it, not the whole page.
<style>
body { margin: 0; }
.bar { height: 10vh; background: #d94a00; color: #fff; padding: 4px; }
.square { width: 30vmin; height: 30vmin; background: #1a5276; margin-top: 8px; }
</style>
<div class="bar">10vh tall</div>
<div class="square"></div>
The square stays a square and is always 30% of the smaller side. Resize the browser window to see what happens in your own page.
Good uses
- Hero sections:
min-height: 100vh. - Text that scales with the screen. See clamp().
vminfor shapes that must stay square on any orientation.
Mobile browsers and dvh
On phones, the address bar slides away when scrolling, so 100vh can be taller than the visible area, and the bottom of your section hides behind the browser’s toolbar. Modern browsers have dvh (“dynamic viewport height”), which follows the visible area:
.hero { min-height: 100vh; min-height: 100dvh; }
The first line is the fallback for older browsers.
100vw and scrollbars
100vw includes the width of a vertical scrollbar, so a full-width element can cause a horizontal scrollbar on desktop. Use 100% for widths when you can.