A viewport is the visible area of a browser window: the reference frame for much of responsive CSS and browser-side geometry. Its coordinate system starts at the top-left corner, (0, 0); x increases rightward, y increases downward, and positions use pixels by default. Coordinates may be negative or extend beyond the visible edges. section 1.4.9 section 10.1
Viewport-relative units size values against the whole visible browser area rather than against a parent element. One vw is 1% of viewport width and one vh is 1% of viewport height, so 100vw by 100vh creates a screen-sized region. This differs from percentage widths, which are relative to a parent. section 1.4.9 section 6.4
Use the viewport as a common reference when a UI must respond to the window itself:
- Measure an element’s current rectangle with getBoundingClientRect(); its x, y, top, left, right, and bottom values are viewport-relative. section 10.2
- Place cursor-driven UI with event.clientX and event.clientY, which remain relative to the visible window rather than the scrolled document. section 13.1.1
- Set responsive breakpoints or bounded scaling from viewport width, using media queries, JavaScript comparisons, or clamp(). section 1.4.6 section 6.5.4
A tooltip that follows the pointer
Read clientX and clientY on mousemove, then add small x and y offsets before assigning left and top. Because both pointer coordinates and positioning use the viewport’s top-left origin, the tooltip stays beside the cursor regardless of document scroll. For document coordinates instead, convert with scrollX and scrollY. section 13.1.1
Mobile browser controls can change the visible height. Small viewport units (sv*) represent the size with browser UI present; large viewport units (lv*) represent it with UI hidden; dynamic units (dv*) update as that UI appears or disappears. For a full-screen region that must match the currently visible height, 100dvh avoids the overflow associated with a changing viewport. section 6.4.4
Table 1.9 Some uses of coordinate systems in CSS and JavaScript
Compare viewport coordinates, DOM measurements, pointer coordinates, and viewport-scaled type in one compact set of CSS and JavaScript tasks. section 1.4.9


