Viewport Units: vw, vh, and the Small, Large and Dynamic Viewport Units
Sizing relative to the browser window: vw, vh, vmin, vmax, vi and vb, the 100vh problem on phones and the small, large and dynamic units that solve it, scrollbars and 100vw, the on-screen keyboard, and accessible viewport-based font sizes.
kvi5hnuCode Sprout team member8 min readOn this page
Viewport units are lengths relative to the size of the viewport, the part of the browser window that shows the page. 1vw is 1% of the viewport's width and 1vh 1% of its height. They let an element fill the screen, or scale with it, wherever it sits in the document.
The basic units
| Unit | 1 unit equals 1% of the viewport's |
|---|---|
vw | width |
vh | height |
vi | size in the inline direction (the width, in horizontal text) |
vb | size in the block direction (the height, in horizontal text) |
vmin | smaller dimension |
vmax | larger dimension |
.hero { min-height: 100vh; } /* at least one screen tall */
.square { width: 80vmin; height: 80vmin; } /* always fits, in portrait or landscape */
.banner { width: 100vw; } /* see the scrollbar caveat below */

Each iframe is a separate viewport, so one stylesheet produces three sizes. vmin switches axis between the portrait and landscape frames, which is what keeps the square on screen.
The units aren't tied to their own axis: width: 50vh is a valid width, half the viewport's height. vi and vb follow the writing mode, so they swap in vertical text (article 47).
Viewport units are relative to the viewport, not to the parent. A 50vw box inside a 300px sidebar is still half the window wide. For sizes relative to a component's container, use percentages or container query units such as cqi (article 78).
The 100vh problem on phones
Mobile browsers show and hide their address bar and toolbars as you scroll, so the visible area changes height constantly. If vh followed those changes, every element sized in vh would resize during scrolling, and the page would jump. So browsers fixed vh to the largest possible viewport, with the toolbars hidden. The result: a height: 100vh hero is taller than the visible screen when the page first loads with the toolbars showing, and its bottom part, often with a call-to-action button, is hidden.
Small, large and dynamic viewports
Three sets of units make the choice explicit:
| Prefix | Viewport | Size |
|---|---|---|
s (svh, svw, svi, svb, svmin, svmax) | small | with all browser toolbars shown: always fully visible |
l (lvh, lvw and so on) | large | with all toolbars hidden: the most space possible |
d (dvh, dvw and so on) | dynamic | the actual current viewport, changing as toolbars move |
none (vh, vw) | default | defined by the browser, in practice the large viewport |
.hero { min-height: 100svh; } /* fully visible on first load */
.app-shell { height: 100dvh; } /* always exactly the visible area */

dvh is the only one that changes: it matches svh while the toolbars show and lvh once they hide.
Guidelines:
svhfor content that must be fully visible, such as a full-screen hero with a button at the bottom. It leaves a small gap when the toolbars hide.lvh(or plainvh) when filling the largest area matters more than the bottom edge being visible at first.dvhfor app-like layouts that must always match the screen exactly, such as a chat window with an input at the bottom. Use it sparingly on long pages: elements sized indvhresize while the user scrolls, which causes layout work and can make content shift.
On desktop browsers, the three are the same, because the toolbars don't move. All these units have worked in every browser since 2022.
The on-screen keyboard
By default, when an on-screen keyboard opens, mobile browsers don't resize the layout viewport; they shrink the visual viewport and scroll it instead, so dvh doesn't change. You can choose another behavior with the interactive-widget key of the viewport meta tag:
<meta name="viewport" content="width=device-width, initial-scale=1, interactive-widget=resizes-content">
| Value | When the keyboard opens |
|---|---|
resizes-visual (default) | only the visual viewport shrinks |
resizes-content | the layout viewport shrinks too, so dvh and layouts adapt |
overlays-content | nothing resizes; the keyboard covers the page |
Chrome and Firefox on Android support it, and Safari on iOS has it in the 27.3 beta. For precise control, JavaScript's VirtualKeyboard and visualViewport APIs report the keyboard's size.
Scrollbars and 100vw
Viewport units traditionally ignore scrollbars. On systems with classic scrollbars, such as Windows and Linux by default, a page that scrolls vertically has a scrollbar taking about 15px, and width: 100vw is that much wider than the space next to it. The result is a small horizontal scrollbar, the classic "full-bleed section causes horizontal scrolling" bug.
Fixes:
- Use
width: 100%instead, when the element's container is already full width. - Since Chrome 145, viewport units subtract the scrollbar when the root element forces or reserves one, with
overflow-y: scrollorscrollbar-gutter: stableonhtml. Other browsers don't do this yet, so keep a fallback. - For full-bleed sections inside a narrow column, a grid with named full-width lines avoids viewport units entirely (article 68).
macOS and mobile devices use overlay scrollbars that take no space, which is why this bug often goes unnoticed during development.
Viewport units for font sizes
Font sizes in viewport units scale smoothly with the window, without breakpoints. But a size in vw alone is an accessibility problem: it ignores the user's default font size, and zooming doesn't enlarge it, because zooming makes the viewport narrower in CSS pixels. WCAG requires text to be resizable to 200%, which pure vw text can fail.
Always combine viewport units with rem, and limit the range with clamp():
h1 { font-size: clamp(1.75rem, 1.2rem + 2.5vw, 3.5rem); }
The rem part keeps the user's settings and zoom in play, the vw part adds scaling, and clamp() sets a minimum and maximum. Article 77 covers fluid typography in detail.
Viewport units in other contexts
- Inside an
<iframe>, the viewport is the iframe, so100vwis the iframe's width. - In print, viewport units are relative to the page area.
- Viewport units don't change with pinch-zoom, which magnifies the visual viewport, not the layout viewport.
What's current
svh,lvh,dvhand the other small, large and dynamic units, plusviandvb, have been Baseline since 2022.- Scrollbar-aware viewport units shipped in Chrome 145 (2026), when the root has
scrollbar-gutter: stableoroverflow-y: scroll. interactive-widgetis in Chrome and Firefox for Android, and in the Safari 27.3 beta.
Common questions
Why does my 100vh hero cut off the button at the bottom on phones?
Plain vh uses the large viewport, which assumes the browser toolbars are hidden. When the page loads, they're shown, so part of the hero is below the visible area. Use 100svh, or min-height: 100svh.
Should I replace every vh with dvh?
No. dvh changes as toolbars appear and disappear, which resizes elements while scrolling. Use it for app-like full-screen layouts, and svh for heroes and sections.
Why does width: 100vw give me a horizontal scrollbar?
On systems with classic scrollbars, 100vw includes the width of the vertical scrollbar. Use 100%, or reserve the gutter with scrollbar-gutter: stable on html, which makes Chrome 145 and later subtract it.
Is font-size: 5vw accessible?
Not on its own: it doesn't respond to the user's font settings or to zoom in the usual way. Combine it with rem inside clamp().
Summary
vw,vh,vi,vb,vminandvmaxare 1% of the viewport's width, height, inline size, block size, smaller and larger dimension.- On phones, plain
vhuses the large viewport, so100vhcan be taller than the visible screen. svh(small),lvh(large) anddvh(dynamic) make the choice explicit. Prefersvhfor heroes anddvhfor app shells.interactive-widgetin the viewport meta tag controls how the on-screen keyboard affects the viewport.100vwincludes classic scrollbars, which causes horizontal overflow. Chrome 145+ subtracts them when the root reserves a gutter.- Never size text with viewport units alone. Mix them with
reminclamp().
Practice questions
A landing page's hero has height: 100vh and a "Shop now" button at its bottom edge. On phones the button is hidden until the user scrolls. Fix it.
Answer
.hero { min-height: 100svh; }
svh uses the small viewport, with the browser toolbars shown, so the whole hero fits on first load. min-height instead of height lets it grow if its content needs more room.
What's the difference between width: 100% and width: 100vw for a direct child of <body>?
Answer
100% is the width of the containing block, the body's content box, which excludes the body's margins and any classic scrollbar. 100vw is the full viewport width, including a classic scrollbar, so it can overflow horizontally and ignores the body's margins.
Why is font-size: 4vw a problem, and what's a better version?
Answer
It ignores the user's default font size and doesn't scale properly with zoom, and it's tiny on phones and huge on wide screens. Better: font-size: clamp(1.5rem, 1rem + 2vw, 3rem);.
A chat app should always fill exactly the visible screen, with the message box at the bottom, including when the keyboard opens on Android. Which CSS and HTML do you use?
Answer
.app { height: 100dvh; } and the viewport meta tag with interactive-widget=resizes-content, so the layout viewport, and with it dvh, shrinks when the keyboard opens.
Previous: Length Units: px, em, rem, ch, lh and the Other Font-Relative Units
Next: Math Functions: calc(), min(), max(), clamp(), round() and Trigonometry

More in CSS Course
Quiz: CSS Delicate Details
Twenty-five questions on the edge cases that surprise experienced developers: invalid custom properties, shorthands that reset values, layer order with !important, containing blocks, margin collapsing, specificity of :is() and :where(), intrinsic minimum sizes, and more, each checked in Chrome.
Quiz: CSS Interview Questions
Thirty front-end interview questions: cascade, specificity and layers, box model and margin collapsing, stacking contexts, positioning, flexbox and grid, responsive design, custom properties, performance, accessibility, modern selectors and architecture.
Quiz: CSS Course Final Exam
Thirty questions across the whole CSS course: syntax and error handling, selectors, the cascade and layers, values and units, color, the box model, backgrounds, positioning, flexbox, grid, responsive design, typography, generated content, effects, transforms, motion and architecture.
Quiz: CSS Architecture, Tooling and Debugging (CSS Part 17)
Twenty questions on modular CSS and BEM, layering a stylesheet, design tokens and theming, pattern libraries and safe refactoring, preprocessors and build tools, the at-rules reference, and debugging common CSS problems.
Discussion
Sign in to ask a question or share what you learned.