Viewport Units and Dynamic Viewports

Intermediate · 6 min

What vw and vh measure, why 100vh breaks on phones, and how the small, large and dynamic viewport units (svh, lvh, dvh) fix it.

Before thisContainer Queries

Kevin Powell · 6 min

Viewport Units and Dynamic Viewports

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Kevin PowellReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • `1vw` is 1% of the viewport's width and `1vh` is 1% of its height. They size things relative to the window, not to a parent element.
  • On phones, the visible area changes as the browser's address bar slides in and out. `100vh` is fixed to the largest case (toolbars hidden), so an element can run underneath the toolbar when it is showing.
  • `svh` is the small viewport (toolbars showing), `lvh` is the large viewport (toolbars hidden) and `dvh` is the dynamic one that tracks the toolbar as it moves. On desktop all four are equal.
  • `100vw` includes the width of a classic vertical scrollbar, so a full-width element can create horizontal scroll. Prefer `width: 100%` or `auto` for layout, and keep `vw` for things that should scale with the window.
  • Type sized only in `vw` doesn't respond to the user's zoom or font settings. Mix it with `rem` inside `clamp()`, which the typography group covers.

Sizing relative to the window

Most CSS lengths relate to something nearby: a parent, a font or a fixed pixel. Viewport units relate to the browser window itself, and 1vw is one percent of its width. 100vh should mean “as tall as the screen”, which is why it is the first thing people reach for to build a full-screen hero.

.hero { min-height: 100vh; }
.big  { font-size: 5vw; }

The units are vw, vh, vmin (the smaller dimension), vmax (the larger), and the logical versions vi and vb for the inline and block axes. They have been in every major browser since 2017.

Why 100vh breaks on phones

On a phone the screen height isn’t constant. Browsers show an address bar and a toolbar when you load a page, and hide them as you scroll. The visible area grows and shrinks.

For years 100vh resolved to the biggest possible size, with the toolbar hidden. On first load the toolbar is showing, so the element is taller than what you can see, and its bottom edge, often a button, hides behind the browser’s own UI. Think of a poster sized for a wall with the curtains open and hung on a wall with them closed.

The fix is three new sets of units, all of which have been in every major browser since late 2022:

Unit Viewport it measures When it matters
svh small: browser UI fully shown the smallest it ever gets
lvh large: browser UI hidden what the old vh means
dvh dynamic: whatever it is now tracks the UI as it moves

Each comes in w, h, i, b, min and max flavors, so svw, dvb and so on exist too. The vh and vw units still behave as they did.

.hero { min-height: 100svh; }   /* never under the toolbar */

dvh is exact but moves: as the toolbar slides away the element resizes, which can cause layout shifts while scrolling. svh is a steadier choice for something like a hero that should fit on first load. Use dvh for things that must fill the screen at all times, like a full-screen app shell or modal.

On a desktop browser, the four heights are the same. In a test at 800 by 600, 100svh, 100lvh, 100dvh and 100vh all measured 600px, so you cannot see the difference without a mobile browser or its emulation.

The scrollbar problem with vw

100vw is the full width of the viewport including a classic vertical scrollbar. The area available for layout is narrower than that when the scrollbar is showing. A width: 100vw element then overflows by the scrollbar’s width and creates horizontal scroll. In a test with a classic scrollbar showing in an 800px-wide window, the content area was 785px and a 100vw element measured 800px, so the page’s scrollWidth was 800. This only happens where scrollbars take up space, which includes most desktop browsers but not phones. Use width: 100% or let block elements fill their container.

Viewport units and accessibility

Text sized in pure vw has a problem: when users zoom the browser (or set a bigger default font size) it doesn’t scale in the way they expect, because the viewport width doesn’t change with text size. Mixing rem into the value restores zoom response:

h1 { font-size: clamp(2rem, 1rem + 4vw, 4rem); }

That is the technique in the fluid typography with clamp lesson. A rule of thumb: use viewport units for large structural sizes and for fluid type inside clamp(), and use %, fr or auto for widths of things in normal layout.

What the video covers

Kevin Powell’s “Use these instead of vh” (November 2023, about six minutes) explores dvh, svh and lvh as alternatives to vh, and mentions the logical variants dvb, svb and lvb for block-size. The description links to an example page and a GitHub repo for testing the units, plus browser-support data. I could not read a transcript, so I do not describe the demo. The runner-up’s description says viewport units can cause horizontal scrolling and accessibility problems, which the 100vw and zoom sections above expand on; the details and numbers are the prose’s own.

Where this fits

Viewport units are one way to make layouts respond to the screen; media queries and container queries are the others. The dvh value shows up in common flexbox patterns for the sticky footer, and the next lesson, intrinsic and fluid layouts, looks at sizing that avoids both viewport units and breakpoints.

Check yourself

  1. 01A hero section has `height: 100vh` and on an iPhone its bottom button sits under the browser toolbar. What is the fix?

    Use `min-height: 100svh` so the section is never taller than the visible area with the toolbar showing, or `100dvh` to track the toolbar as it moves. `100vh` equals the large viewport, which assumes the toolbar is hidden.

  2. 02What is the difference between `svh`, `lvh` and `dvh`?

    `svh` is the smallest the viewport gets (browser UI fully shown), `lvh` is the largest (UI hidden), and `dvh` is whichever it currently is, updating as the UI appears and disappears. `dvh` is the most accurate and can cause relayout while scrolling, so `svh` is a stable choice for things like a hero.

  3. 03You set `width: 100vw` on a banner and a horizontal scrollbar appears on desktop. Why?

    `100vw` includes the width of the vertical scrollbar, but the page content area is narrower than that. The banner is wider than the space it sits in, so it overflows. Use `width: 100%` or let a block element fill its container.

  4. 04On a desktop browser, how different are `100svh`, `100lvh`, `100dvh` and `100vh`?

    Not at all. Desktop browsers have no collapsing toolbar, so all four were 600px in a 600px-tall window in a test. The differences only show on mobile browsers with dynamic UI.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark