More CSS answers

by Danny
Published 2 min read

On mobile, 100vh includes space hidden behind the browser toolbar, so full-height layouts overflow. The svh, lvh and dvh viewport units fix this. Here is when to use each.

The short answer

#

On mobile browsers, 100vh is measured as if the address bar and toolbars were hidden, so it is taller than the visible screen when they are showing. Use the newer viewport units instead: 100svh for the smallest visible height, or 100dvh to track the toolbar as it moves.


_10
.hero {
_10
min-height: 100vh; /* fallback for very old browsers */
_10
min-height: 100svh;
_10
}

Why it happens

#

Mobile browsers show and hide their address bar and toolbar as you scroll. If vh changed every time that happened, every element sized with it would resize mid-scroll, causing jank and layout shifts. So browsers chose to keep vh fixed at the largest possible viewport height, the one you get when the toolbars are retracted.

When the page first loads, the toolbars are usually visible. A 100vh section is then taller than the space on screen, and the bottom of it, often a call to action button, is hidden behind the browser chrome.

The CSS Values and Units specification added three sets of units to make the choice explicit:

UnitMeansChanges on scroll?
svhsmall viewport height, toolbars showingNo
lvhlarge viewport height, toolbars hiddenNo
dvhdynamic, whichever is currentYes

There are matching width units (svw, lvw, dvw) and logical versions (svb, svi and so on). These units are supported in all current major browsers and have been for several years.

How to fix it

#

Use svh for full-screen sections

For a hero or splash section that should fit on screen when the page first loads, svh is the safest choice. It never changes while scrolling, so nothing jumps.


_10
.hero {
_10
min-height: 100svh;
_10
}

The trade-off is a small gap at the bottom once the toolbar retracts. For most layouts that is fine.

Use dvh for app shells and fixed panels

If you are building an app-like layout where a footer or input bar must always sit at the very bottom of the visible area, dvh follows the real height:


_10
.app {
_10
height: 100dvh;
_10
display: grid;
_10
grid-template-rows: auto 1fr auto;
_10
}

Because dvh changes while the user scrolls, avoid it on long, content-heavy pages. Resizing large areas during scroll can be expensive and visibly jumpy.

Keep a vh fallback if you need it

Browsers ignore declarations they do not understand, so listing vh first and the new unit second gives older browsers a reasonable fallback:


_10
.panel {
_10
height: 100vh;
_10
height: 100dvh;
_10
}

Drop the JavaScript workaround

Before these units existed, a common fix was to measure window.innerHeight and store it in a custom property:


_10
// Old workaround: no longer needed in current browsers
_10
const setVh = () => {
_10
document.documentElement.style.setProperty('--vh', `${window.innerHeight * 0.01}px`);
_10
};
_10
window.addEventListener('resize', setVh);
_10
setVh();

You can remove this and use svh or dvh instead. It runs on every resize and can still be out of step with the toolbar.

Common pitfalls

#
  • Using height where min-height is better. A fixed height: 100svh cuts off content on short screens or with large text. Prefer min-height unless the layout must be exactly one screen.
  • Reaching for dvh everywhere. It is the most accurate but also the only one that moves. Start with svh.
  • Forgetting the on-screen keyboard. By default, the virtual keyboard does not resize these units in most mobile browsers. Look at the interactive-widget key in the viewport meta tag if you need the layout to respond to it.
  • Expecting a difference on desktop. On desktop browsers without retracting toolbars, all three units are the same as vh.
#

Was this resource helpful?