Skip to content
Tutorials Center
My learning

Topics

Lesson 21 of 139 ยท Values and Units

Viewport Units

Learn CSS viewport units, from vw and vh to vmin, vmax and the new svh, lvh and dvh units that fix full-height layouts behind mobile address bars.

Viewport units measure lengths as a share of the browser window. 50vw is half the window's width, 100vh is its full height. They let you build things that relate to the screen itself: a hero section that fills the first view, a headline that grows on wide monitors, a modal that never gets taller than the window.

They're simple at first glance, but they come with two famous traps: 100vw causes horizontal scrollbars, and 100vh is too tall on phones. This lesson covers the classic units, the newer small, large and dynamic units that fix the phone problem, and when to reach for something else entirely.

The four classic units

Unit Equals
vw 1% of the viewport's width
vh 1% of the viewport's height
vmin 1% of the smaller of width and height
vmax 1% of the larger of width and height

The viewport is the visible area of the page: the browser window minus its toolbars. On a 1280 ร— 720 window, 10vw is 128px, 10vh is 72px, 10vmin is 72px and 10vmax is 128px.

Unlike percentages, viewport units don't care about the parent element. A box that is 50vw wide is half the window wide, even if it sits inside a narrow sidebar.

In the live editors on this site, the preview is an <iframe>, and an iframe is its own viewport. So in every example below, vw and vh refer to the preview pane, not your whole browser window. Resize your browser and the bars will change.

vw, vh, vmin and vmax

vmin is the useful one of the last two: a square of 80vmin always fits on screen, whether the device is held upright or sideways.

Trap 1: 100vw and the scrollbar

On desktop systems with classic scrollbars (most Windows setups, and macOS with "always show scrollbars"), vw measures the window including the vertical scrollbar. So width: 100vw on a page that scrolls is wider than the space available, by exactly the width of the scrollbar, and you get a horizontal scrollbar too.

CSS
/* Risky: overflows by the scrollbar's width on many desktops */
.full-bleed { width: 100vw; }

/* Usually what you meant: */
.full-bleed { width: 100%; }

For a block in normal flow, width: auto or 100% already fills the available width. Reach for vw only when you need to break out of a narrow parent, and even then test on a system with visible scrollbars.

Trap 2: 100vh on phones

Mobile browsers have address bars and toolbars that slide away as you scroll. That means the viewport has two heights: a smaller one while the toolbars are visible, and a larger one when they're hidden. Years ago, browsers decided that vh would use the larger height, to avoid content jumping around while you scroll.

The result: a section with height: 100vh is taller than the screen when the page first loads. Its bottom, often where the "Get started" button sits, is hidden behind the toolbar.

Small, large and dynamic viewport units

Newer units let you choose which height you mean:

Units Measures Behavior
svh, svw, svmin, svmax The small viewport, toolbars shown Never hidden behind UI; may leave a gap when toolbars hide
lvh, lvw, lvmin, lvmax The large viewport, toolbars hidden Same as the old vh
dvh, dvw, dvmin, dvmax The dynamic viewport, whatever is visible now Changes as toolbars slide in and out

There are also logical versions: vi and vb (inline and block size of the viewport), with svi, lvb and so on. In horizontal writing, vi matches vw and vb matches vh.

All of these are supported in every major browser since late 2022. On desktop, where toolbars don't slide, all three versions give the same number.

Full-height hero with a fallback

Notice the pattern of writing 100vh first and 100svh second. A browser that doesn't understand svh drops the second declaration and keeps the first. A newer one uses the second because it comes later.

Which one should you use?

  • svh for most full-screen sections. The content is guaranteed to fit when the page loads. When the toolbars hide, there may be a little extra space at the bottom, which nobody minds.
  • dvh for app-like layouts that must always exactly match the visible area, such as a chat screen with the message box pinned to the bottom. Be aware that the element resizes while the user scrolls, which can cause content below it to shift and costs some performance.
  • lvh rarely, perhaps for a background image that should cover the screen even when toolbars hide.

Also prefer min-height over height for full-screen sections. If the text is longer than the screen, or the user zooms in, min-height lets the section grow instead of letting text spill out.

The on-screen keyboard is a different story. By default, most mobile browsers don't shrink the viewport units when the keyboard opens. Plan forms so they still work when the bottom of the screen is covered.

Viewport units for type: handle with care

It's tempting to size headings in vw so they grow with the window. Pure vw font sizes have two serious problems:

  1. They ignore zoom. When a user zooms in with Ctrl and +, px and rem text gets bigger, but the viewport also gets "smaller" in CSS pixels, so 5vw text barely changes. That can fail accessibility requirements for resizable text.
  2. They have no limits. 5vw is a tiny 16px on a phone and a huge 128px on a large monitor.

The fix is to mix a rem part (which respects zoom and user settings) with a small vw part, and clamp the result:

Fluid heading: vw alone vs clamp()

Resize your browser and compare. clamp() is the subject of calc(), min(), max() and clamp(), and Fluid Typography builds a whole type scale this way.

When not to use viewport units

Viewport units describe the window. Most components should respond to the space they have, not the window: a card in a narrow sidebar shouldn't get bigger text just because the window is wide. For those cases:

  • Use %, fr and flexible layouts (Flexbox and Grid) for widths.
  • Use container query units such as cqi (1% of the container's inline size), covered in Container Queries.

Keep viewport units for things that really relate to the screen: full-screen sections, fixed overlays, sticky headers, and the fluid part of a clamped size.

env() and safe-area insets

Many phones don't have a rectangular screen. Rounded corners, a camera notch or "island" at the top, and a home indicator bar at the bottom all cut into the display. By default, browsers keep your page inside a safe rectangle and paint the leftover strips with the page background, so nothing gets hidden.

For an edge-to-edge design, you can opt out of that letterboxing with viewport-fit=cover in the viewport meta tag (see Responsive Design for the rest of this tag):

HTML
<meta name="viewport" content="width=device-width, initial-scale=1, viewport-fit=cover">

Now your page fills the whole screen, including the areas behind the notch and the home indicator. To keep text and buttons out of those areas, the browser tells you how big they are through environment variables, which you read with the env() function:

Variable Distance from the edge that is unsafe
safe-area-inset-top Top: status bar, notch or camera cutout
safe-area-inset-right Right: the notch when the phone is in landscape
safe-area-inset-bottom Bottom: the home indicator
safe-area-inset-left Left: the notch in the other landscape direction

env() works like var(), but the values come from the browser instead of your stylesheet, and they're the same for every element. It accepts a fallback as a second argument, and it works inside calc(), max() and the other math functions:

CSS
.bottom-bar {
  position: fixed;
  inset-inline: 0;
  bottom: 0;
  /* 12px of normal padding, plus room for the home indicator */
  padding-bottom: calc(12px + env(safe-area-inset-bottom, 0px));
}

.page {
  /* At least 16px of padding, more where the notch is */
  padding-inline: max(16px, env(safe-area-inset-left)) max(16px, env(safe-area-inset-right));
}

The two patterns differ on purpose. Adding the inset with calc() keeps your normal spacing on top of the unsafe area, which suits a bar whose background should extend under the home indicator. Using max() takes whichever is bigger, so content isn't pushed in twice as far: 16px on a normal screen, and just the notch width where there is one.

On a device without cutouts, and on desktop, every inset is 0px. The fallback is only used when the browser doesn't define the variable at all. That makes these rules safe to add everywhere. Since you can't see a notch in this editor, the demo below fakes one: the checkbox swaps the real env() values for typical phone values so you can watch the layout adapt.

Safe-area padding on a simulated phone

On a laptop the last line reports 0px, not the 99px fallback, because the browser defines the variable even when there's nothing to avoid. Untick the box to see the layout with zero insets: the same CSS, just smaller paddings.

A few more environment variables exist for special cases. keyboard-inset-* describes the on-screen keyboard when a page opts in with the VirtualKeyboard API (Chromium only), and titlebar-area-* helps installed web apps that draw their own title bar. env() itself, and the four safe-area variables, have worked in all major browsers since 2019.

Check your understanding

Quick check ยท 5 questions

of 5 answered You scored / 5 ยท perfect!

  1. 1. In a 1000 ร— 600 viewport, how wide is a box with width: 50vmin?

    vmin uses the smaller dimension, 600px, so 50vmin is 300px.
  2. 2. Why does width: 100vw sometimes create a horizontal scrollbar on desktop?

    With classic scrollbars, 100vw is the whole window including the scrollbar, so it's wider than the space next to it.
  3. 3. Which unit keeps a full-height hero fully visible on phones while the toolbars are showing?

    svh measures the small viewport, the area left when the browser's toolbars are visible.
  4. 4. Why write min-height: 100vh; before min-height: 100svh;?

    Invalid declarations are ignored, so browsers without svh support use the earlier line, while newer ones let the later line win.
  5. 5. What's the main accessibility problem with font-size: 5vw?

    Zooming shrinks the viewport in CSS pixels, so pure vw text hardly changes. Mix in rem and use clamp() instead.

Summary

  • vw and vh are 1% of the viewport's width and height; vmin and vmax use the smaller or larger side.
  • In this site's editors, the viewport is the preview iframe.
  • 100vw can overflow by the scrollbar's width; 100% is usually what you want.
  • On phones, vh uses the large viewport, so 100vh sections hide behind toolbars.
  • svh (small), lvh (large) and dvh (dynamic) let you choose; svh is the safe default, with a vh fallback line first.
  • Don't size text in vw alone: combine rem and vw inside clamp().

Next: Colors โ€” named colors, hex, rgb(), hsl(), transparency and currentColor.