Viewport units

Also known as: vw · vh

On this page 7

Viewport units are CSS length units that express a size as a percentage of the browser viewport, the visible area in which a page is rendered. The best known are vw and vh: 1vw equals 1% of the viewport width and 1vh equals 1% of the viewport height, so 100vh describes an element exactly as tall as the window.

Because they respond to the window rather than to a parent element, viewport units are a core tool of responsive web design in CSS. They make full-screen sections, fluid typography and proportional spacing possible without media queries, although mobile browsers introduced complications that led to the newer small, large and dynamic viewport units.

How viewport units are calculated

A viewport unit is resolved by multiplying one hundredth of a viewport dimension by the number in front of it. On a window 1,200 pixels wide, 50vw computes to 600 pixels; if the user resizes the window to 800 pixels, the same declaration becomes 400 pixels without any extra code.

The reference is the initial containing block, which normally matches the layout viewport. A key detail is that vw includes the width of a vertical scrollbar on desktop systems with classic scrollbars, so 100vw can be slightly wider than the space actually available to content and may cause a horizontal scrollbar.

The following table lists the original viewport units defined in the CSS Values and Units specification.

UnitReference dimensionExample on a 1440 by 900 window
vw1% of viewport width10vw = 144px
vh1% of viewport height10vh = 90px
vmin1% of the smaller dimension10vmin = 90px
vmax1% of the larger dimension10vmax = 144px
vi1% of the inline-axis size10vi = 144px in horizontal text
vb1% of the block-axis size10vb = 90px in horizontal text
Classic viewport units and their reference dimensions

The logical units vi and vb follow the writing mode. In vertical scripts such as traditional Japanese layout, the inline axis runs vertically, so vi then refers to the height rather than the width.

Small, large and dynamic viewport units

Mobile browsers created a long-standing problem: the address bar and toolbars expand and collapse while the user scrolls, so the viewport height changes constantly. Browsers resolved classic vh against the largest possible viewport, which meant a 100vh hero section was taller than the visible screen when the toolbars were shown.

To solve this, the CSS working group introduced three viewport size variants, supported in all major browsers since 2022 and 2023. Each variant exists for width, height, inline, block, minimum and maximum, for example svh, lvh, dvh, svw and dvmin.

VariantUnitsViewport assumedBest suited for
Small viewportsvh, svwBrowser toolbars fully expandedContent that must always fit on screen
Large viewportlvh, lvwBrowser toolbars fully retractedBackgrounds that may extend under toolbars
Dynamic viewportdvh, dvwCurrent state, updated liveFull-screen panels that track the toolbar
Default viewportvh, vwDefined by the browser, usually largeDesktop layouts and legacy code
Viewport size variants on mobile browsers

The dynamic units are not free of cost. Because dvh changes value during scrolling, layouts that depend on it may be recalculated repeatedly, which can cause visible jumps and extra layout work. Many developers therefore prefer svh for elements that must never be cut off and reserve dvh for cases that truly need live updates.

How viewport units compare with other CSS length units

Viewport units belong to the family of relative length units, but each relative unit answers to a different reference. Choosing the right one depends on whether a size should follow the window, the parent element, the font size or the containing component.

UnitRelative toTypical use
vw, vhBrowser viewportFull-screen sections, fluid spacing
%Parent element dimensionColumns and widths inside a container
remRoot element font sizeAccessible typography and spacing
emCurrent element font sizeComponent-scaled padding and icons
cqw, cqhNearest query containerComponents that adapt to their slot
Viewport units compared with related relative units

The container query units cqw and cqh are the closest relatives. They work like viewport units but measure the nearest container declared with container-type, which makes them better for reusable components placed in sidebars, cards or grids of varying width.

Practical patterns with viewport units

Viewport units are most effective when combined with clamping functions and other units. The clamp() function sets a minimum, a preferred value and a maximum, so a viewport-based size cannot grow or shrink beyond sensible limits.

  • Fluid typography: font-size: clamp(1rem, 0.8rem + 1vw, 1.5rem) scales text smoothly between a readable minimum and a comfortable maximum.
  • Full-height sections: min-height: 100svh creates a hero area that fits the screen even with mobile toolbars visible.
  • Proportional spacing: padding-inline: 5vw keeps side margins proportional to the window width on large displays.
  • Square elements: width: 50vmin; height: 50vmin produces a square that fits in both portrait and landscape orientation.

Using min-height instead of height is an important safeguard. A fixed viewport height clips content when the text is longer than expected or when the user enlarges the font, whereas a minimum height lets the section grow naturally.

Limitations and accessibility risks

The most serious risk is text sized purely in vw. Such text does not respond to browser zoom in the expected way, because zooming changes the CSS pixel width of the viewport only partly, and this can violate the WCAG 1.4.4 requirement that text be resizable to 200 percent.

Other frequent problems include the scrollbar overflow caused by 100vw, content hidden by the on-screen keyboard on mobile devices, and nested iframes in which the viewport is the frame rather than the main window. Combining viewport units with rem inside clamp() and testing on real devices addresses most of these issues.

Stripped to the essentials

Viewport units let a web designer size things in proportion to the screen. Writing 100vh means “as tall as the window”, and 50vw means “half as wide as the window”. When the window gets bigger or smaller, those elements resize automatically.

Think of it like describing furniture as a share of a room rather than in centimeters. The phone version is trickier because its toolbars slide in and out, which is why newer units measure the small, large or current screen separately.

Where it is applied

A travel agency rebuilds the landing page of its booking site. The design calls for a full-screen photograph with a search form on top. The first version uses height: 100vh, but testers on smartphones report that the search button is hidden below the visible area whenever the address bar is showing.

The front-end developer replaces the rule with min-height: 100svh, so the section is measured against the small viewport and the form always appears above the fold. The headline uses clamp(2rem, 1.2rem + 3vw, 4rem), which keeps it legible at 360 pixels and prevents it from becoming oversized on a 2560-pixel monitor. After the change, the search form is fully visible on every tested device, and text still scales correctly when users zoom to 200 percent.