Skip to content

This notebook belongs to

Sadat Jubayer

Senior Software Engineer at Premio

If found, please return to hello@jubayer.dev

Vol. 7Seven years of building

Started
January 2020
Everyday tools
TypeScript, Vue, React, Next.js, NestJS, React Native
Education
B.Sc. CSSE, AIUB, 2020
1 min readCSS

Fix mobile 100vh with dvh units

Use dvh to size a mobile section around browser toolbars, or svh to keep its minimum height stable.

Your hero section has height: 100vh, and on a phone its bottom edge sits behind the browser's address bar until you scroll. The button you put at the bottom is there, and you cannot tap it.

.hero {
  height: 100vh;
}

Phone browsers slide the toolbar away as you scroll, so the visible area changes size while the page is open. The spec and the MDN docs define vh against one size, the viewport with the toolbars retracted. With the toolbar showing, 100vh is taller than the screen you can see.

Three units for three states

Viewport height comes in three sizes, each with its own unit:

  • lvh is the large viewport, toolbars retracted. vh matches it.
  • svh is the small viewport, toolbars showing.
  • dvh is the dynamic viewport, which changes as the toolbars expand or retract.

For a full-height section I use dvh, and I add the old unit on the line above it. I write min-height instead of height so a section with a lot of content grows instead of overflowing.

.hero {
  min-height: 100vh;
  min-height: 100dvh;
}

A browser that does not know dvh drops the second line as invalid and keeps 100vh. I checked that with a made-up unit in Chromium: the computed min-height stayed at the 100vh value, 664px in a 390 by 664 window.

Picking one

dvh follows the toolbar. Browsers may snap between sizes during the toolbar animation, and the on-screen keyboard can overlay the page without changing these units. If that movement bothers you, svh gives a fixed height that always fits with the toolbar showing. lvh gives the old 100vh behaviour under a name that says what it means.

To see the difference yourself, open the page on a phone, give the section a coloured background, and scroll until the toolbar shrinks. Watch where the bottom edge of the colour sits against the screen edge for each unit.