CSS Viewport Units Explained: vw, vh, vmin, vmax, dvh, svh and lvh
Viewport units let you size things relative to the browser window instead of a parent element: a hero that fills the screen, a heading that grows with the window, a panel that’s always half the screen tall. They’re easy to use, but they come with two well-known traps, one on mobile and one on desktop.
The basic units
| Unit | Equals |
|---|---|
| 1vw | 1% of the viewport width |
| 1vh | 1% of the viewport height |
| 1vmin | 1% of the smaller of width and height |
| 1vmax | 1% of the larger of width and height |
On a 1440 × 900 browser window, 10vw = 144px and 10vh = 90px. On a 390 × 844 phone, 10vw = 39px. The viewport unit converter above converts both ways for common device sizes.
There are also logical versions, vi and vb (inline and block direction), which follow the writing mode, useful for vertical languages.
The conversion formulas
- px = value × viewport dimension ÷ 100
- value = px ÷ viewport dimension × 100
48px on a 1440px-wide screen is 48 ÷ 1440 × 100 = 3.333vw. The same 3.333vw on a 390px phone is just 13px, which shows why viewport units need limits.
Trap 1: 100vh on mobile
On mobile browsers, the address bar and toolbar slide in and out as you scroll. Originally, browsers defined 100vh as the height with toolbars hidden, the largest possible viewport, so that layouts wouldn’t jump while scrolling. The side effect: a height: 100vh section is taller than the visible screen when the page first loads, and content at the bottom (like a call-to-action button) is hidden behind the browser toolbar.
The fix: small, large and dynamic viewport units
New units, supported in all major browsers since 2022–2023, describe each case explicitly:
| Unit | Based on | Use |
|---|---|---|
svh (small) | Viewport with browser UI fully shown | Content that must always be fully visible |
lvh (large) | Viewport with browser UI hidden | Same as the old vh behaviour |
dvh (dynamic) | Current viewport, updates as UI shows/hides | Full-screen sections that should always fit |
There are matching svw, lvw, dvw, svmin and so on.
.hero { min-height: 100vh; min-height: 100dvh; }The first line is a fallback for older browsers. dvh can cause content to resize while the user scrolls, so for many layouts svh is the calmer choice: always fits, never jumps.
Trap 2: 100vw causes horizontal scrolling
On desktop operating systems with classic scrollbars (Windows, Linux, and macOS with “always show scroll bars”), 100vw includes the vertical scrollbar’s width. An element set to width: 100vw is therefore about 15–17px wider than the visible content area, creating a horizontal scrollbar.
Fixes:
- Use
width: 100%for full-width elements inside the page flow. - For “full-bleed” elements breaking out of a centered container, use techniques based on the container’s width, or CSS Grid with named full-width columns.
scrollbar-gutter: stablereserves scrollbar space consistently, which reduces layout jumps but doesn’t change what 100vw means.
Typography with viewport units
Sizing text purely in vw is a classic mistake. It becomes tiny on phones and huge on wide monitors, and it doesn’t respond properly to browser zoom, which can fail WCAG 1.4.4 (Resize Text). Combine vw with rem inside clamp():
h1 { font-size: clamp(2rem, 1.2rem + 3vw, 4rem); }Our fluid typography guide explains the formula.
Container query units
Viewport units are relative to the whole window, but components often live in containers of different widths: a card might appear in a narrow sidebar or a wide main column. Container query units solve this:
| Unit | Relative to |
|---|---|
cqw / cqh | 1% of the query container’s width / height |
cqi / cqb | 1% of the container’s inline / block size |
cqmin / cqmax | Smaller / larger of cqi and cqb |
Set container-type: inline-size on the parent, and children can size text and spacing with cqi instead of vw. Supported in all major browsers since 2023.
Percentages versus viewport units
| Unit | Relative to | Typical use |
|---|---|---|
| % (width) | Parent’s width | Columns, images inside containers |
| % (height) | Parent’s height, only if the parent has a defined height | Often confusing; prefer other methods |
| vw / vh | Browser viewport | Full-screen sections, fluid type with clamp() |
| dvh / svh | Viewport accounting for mobile UI | Mobile full-height layouts |
| cqi | Component’s container | Responsive components |
Percentage heights often “don’t work” because the parent has no explicit height. Viewport units sidestep this, which is why min-height: 100vh became the go-to way to make full-height layouts.
Hero sections, sticky footers and fluid gaps
- Full-screen hero on mobile:
min-height: 100svh;(with a100vhfallback). - Sticky footer layout: body as a flex column with
min-height: 100dvh, main contentflex: 1. - Fluid section padding:
padding-block: clamp(3rem, 8vh, 8rem). - Modal max height:
max-height: 90dvh; overflow: auto;. - Square that fits the screen:
width: 80vmin; height: 80vmin;.