Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / CSS

vw — viewport width unit

The vw unit expresses a length relative to the large viewport width: 1vw is 1% of that width, independently of the parent's width.

Version: CSS modules; structured CSSWG indexes reconciled with pinned MDN BCD; Chromium 152 scope gate, per-example verification still required

Syntax

<number>vw

vw is a relative CSS length unit. Write a number directly before the unit, such as 50vw. 1vw equals 1% of the large viewport width, so 50vw means half that width. It works in properties accepting a length, not just width.

Unlike width: 50%, which uses the containing block's width as its basis, width: 50vw does not depend on the parent's width. The unit refers to physical width regardless of writing direction.

vw corresponds to lvw: it assumes dynamically retractable browser interface is hidden. svw uses the small viewport, while dvw uses the dynamic viewport. With overflow: auto, scrollbars do not reduce the viewport-unit basis, so 100vw can cause horizontal overflow. The example disables scrollbars to isolate the width calculation.

Examples

<!doctype html>
<html lang="en">
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <title>Measuring 50vw</title>
  <style>
    html { overflow: hidden; }
    body { margin: 0; }
    #container { width: 25%; }
    #bar { width: 50vw; height: 40px; background: teal; }
    #reference { width: 100lvw; }
  </style>
</head>
<body>
  <h1>A bar sized in viewport units</h1>
  <div id="container" aria-hidden="true"><div id="bar"></div></div>
  <div id="reference" aria-hidden="true"></div>
  <script>
    const container = document.getElementById('container');
    const bar = document.getElementById('bar');
    const width = element => element.getBoundingClientRect().width;
    const firstParentWidth = width(container);
    const firstBarWidth = width(bar);
    container.style.width = '75%';
    const largeViewportWidth = width(document.getElementById('reference'));
    console.log(`Bar width is half the large viewport width: ${Math.abs(firstBarWidth - largeViewportWidth / 2) < 0.03125}`);
    console.log(`Changing the parent width leaves the bar width unchanged: ${width(container) > firstParentWidth && width(bar) === firstBarWidth}`);
  </script>
</body>
</html>

Result

Bar width is half the large viewport width: true
Changing the parent width leaves the bar width unchanged: true

The displayed code and declared result were verified in an isolated environment using the method recorded in the verification evidence for this exact version.

Sources