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
| Unit | Value | Meaning |
|---|---|---|
1vh | 1% of window height | Height in 1/100 of viewport height |
1vw | 1% of window width | Width in 1/100 of viewport width |
1vmin | 1% of smaller value between vh or vw | Minimum of width/height |
1vmax | 1% of larger value between vh or vw | Maximum 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
vhandvw— 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
vhheight 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.