Why 100vh Breaks Mobile Forms (and How dvh + visualViewport Fix It)
Every frontend developer runs into the same mobile layout surprise sooner or later: a form or modal looks clean on a desktop browser at 390px width, yet on an actual phone the bottom submit button slides behind the brows
Every frontend developer runs into the same mobile layout surprise sooner or later: a form or modal looks clean on a desktop browser at 390px width, yet on an actual phone the bottom submit button slides behind the browser toolbar or disappears underneath the on-screen keyboard.
The root cause is almost always height: 100vh.
What 100vh Actually Measures on Mobile
Classic vh units represent 1% of the large viewport height (the height when the browser's address bar and bottom navigation bar are fully retracted). When a user first loads a page, both browser bars are visible, so 100vh is taller than the actual visible screen. Any sticky footer or action bar anchored to the bottom of a 100vh container immediately gets clipped.
Step 1: Switch Full-Screen Containers to dvh and svh
Modern mobile browsers support three dynamic viewport units:
-
100svh(Small Viewport Height): the visible height when browser chrome is expanded. Good for above-the-fold hero sections that should never jump during scrolling. -
100lvh(Large Viewport Height): matches legacy100vh. -
100dvh(Dynamic Viewport Height): adjusts automatically as the browser address bar expands or retracts.
A safe CSS pattern keeps a fallback for older engines while upgrading modern mobile browsers:
.app-shell {
min-height: 100vh;
min-height: 100dvh;
display: flex;
flex-direction: column;
}
.app-main {
flex: 1 1 auto;
overflow-y: auto;
padding-bottom: env(safe-area-inset-bottom, 16px);
}
Step 2: Handle the On-Screen Keyboard With visualViewport
Even 100dvh does not solve every soft-keyboard quirk across iOS Safari and Android WebViews. On iOS, opening the keyboard shifts the visual viewport without always shrinking the layout viewport.
Listening to window.visualViewport lets you expose the exact visible height as a CSS custom property:
function syncViewportHeight() {
const vv = window.visualViewport;
const visibleHeight = vv ? Math.round(vv.height) : window.innerHeight;
document.documentElement.style.setProperty("--visible-vh", visibleHeight + "px");
}
if (window.visualViewport) {
window.visualViewport.addEventListener("resize", syncViewportHeight);
window.visualViewport.addEventListener("scroll", syncViewportHeight);
}
window.addEventListener("resize", syncViewportHeight);
syncViewportHeight();
Then bind modal wrappers or bottom-docked form drawers to max-height: var(--visible-vh, 100dvh).
Quick Checklist Before Shipping Any Mobile Form
- Replace bare
height: 100vhwithmin-height: 100dvhso content can still scroll when a keyboard takes half the screen. - Add
env(safe-area-inset-bottom)padding to fixed footers on gesture-bar phones. - Keep primary submit buttons inside the scrollable flow or anchored to
--visible-vhso narrow 360px screens never trap the user.
Three small CSS and viewport habits eliminate most mobile layout bugs before they ever reach production.
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.