{"canonical_url":"https://engineer.company/glossary/viewport/","content_markdown":"# Viewport\n\n## What it is\n\nA viewport is the rectangle a browser lays a document out in and paints it into:\non a screen, the area inside the window's own toolbars; on paper, the page area\nof each sheet. CSS measures against it constantly. The initial containing block,\nthe box the root element is laid out in, takes its size, a `width` or `height`\nmedia query asks about it, and the `vw` and `vh` units are hundredths of it. On\na phone the word, and its plural viewports, needs a qualifier, and the\nspecifications supply several: layout, visual, small, large and dynamic.\n\nOn a desktop the viewport and the window are nearly the same thing. On a phone\nthey part company. Because so many documents were designed for desktop windows,\nmobile browsers lay out a page that gives no instructions at a fixed width that\nmimics a desktop window, typically 980 to 1,024 CSS pixels according to the CSS\nViewport specification, and scale the result down to fit the glass. A page opts\nout with one element in its head, which accepts `width`, `height`, three scale\nlimits, `user-scalable` and `interactive-widget`:\n\n```html\n\u003cmeta name=\"viewport\" content=\"width=device-width, initial-scale=1\" /\u003e\n```\n\nZoom splits the viewport in two. The layout viewport is the one CSS lays the\npage out against, and pinch-zoom leaves it alone, so line breaks stay put. The\nvisual viewport is the part of the page on the glass at that moment: it shrinks\nas the reader zooms in and slides as they pan. By default an on-screen keyboard\nshrinks only the visual viewport; `interactive-widget=resizes-content` asks the\nbrowser to resize the layout as well, and `overlays-content` asks it to resize\nneither.\n\nPhones add browser chrome that slides away. The address bar takes space when a\npage loads and retracts as the reader scrolls, so the visible height changes\nunder a page that has not moved. CSS Values 4 names three sizes for this: the\nlarge viewport assumes every retractable toolbar is retracted, the small one\nassumes every one is shown, and the dynamic one follows whichever is true. The\nunits carry the prefixes, `lvh`, `svh` and `dvh`, with matching widths, and\nplain `vh` and `vw` are defined against the large viewport. An interface that\noverlays content without shifting it, typically the on-screen keyboard, changes\nnone of these lengths.\n\n## Why it matters\n\nResponsive design is built on the viewport. A width breakpoint in a stylesheet is\na question about its width, so a page that omits the `meta` element is laid out\nat desktop width on a phone and shrunk until its text is unreadable, however\ncareful its media queries are.\n\nHeight is the hard axis. Width can change with rotation or resizing; height can also change\nwhen the toolbar slides, when the keyboard opens and when the phone turns, so\nanything sized to the screen's height has to say which height it means. The\nsmall viewport allows content to fit with retractable toolbars shown and leaves\na strip of empty space when those toolbars retract. The large one fills the screen and puts content behind the toolbars\nwhile they show. The dynamic one accounts for changes in the toolbar state, and the specification itself warns\nthat it can resize content while the reader scrolls, which moves everything\nbelow.\n\nScreens also have corners. A phone with rounded corners or a camera cutout is\nnot a rectangle, and with the default, `auto`, the whole page stays viewable on\nit. Adding `viewport-fit=cover` asks for the viewport to be the rectangle drawn\naround the whole physical screen, and hands the page the job of keeping text\nclear of the cutout with the `env(safe-area-inset-*)` variables. Those insets\nmust be zero on a rectangular display, so one padding rule serves both kinds.\n\nFixed positioning hangs on the viewport too. A fixed box is placed against the\nlayout viewport, which CSS Positioned Layout sizes to match the dynamic\nviewport, so cookie bars, modal backdrops and full-screen menus hold still while\nthe page scrolls. Each assumes no ancestor has taken that role, and `transform`,\n`will-change` and `contain` can.\n\nPrint is the viewport a screen-only stylesheet never meets. On paper the\nviewport units measure the page area, a `width` media query asks about the page\nbox, and a fixed element repeats on every page.\n\n## What it looks like in practice\n\nThe pages on this site declare `width=device-width, initial-scale=1,\nviewport-fit=cover`, so they lay out at the phone's width and run edge to edge\nbeneath a notch. The stylesheet pays for the second half: side padding is\n`max(1rem, env(safe-area-inset-left))` and its right-hand twin, so text keeps a\nrem from the edge and moves further in only where a cutout is. On short\nlandscape screens the floor rises to `max(2rem, 7vw,\nenv(safe-area-inset-left))`, since the insets are zero on a phone without a\ncutout and one rem sat too close to the edge.\n\nFull-screen sections take their height from a shared variable set to `100dvh`,\nwith `100vh` as the fallback, and the comment beside it records why: `100vh` can\nbe taller than what shows while the address bar is out, and it had let the next\nsection peek in under the hero. Two elements deliberately ignore it. The\nmasthead mark is sized in static `vh`, because a mark keyed to the dynamic\nviewport would resize as the browser chrome slid away and reflow the hero in\nmid-scroll. The fixed background on touch screens is `100lvh` tall, so it never\nresizes as the address bar comes and goes, and it sits on its own compositing\nlayer, because touch browsers tore it into light and dark bands during momentum\nscrolling.\n\nAn earlier home-page layout gave each service block a minimum height of `100dvh`,\nso it occupied one screen while its content was shorter than the screen. The stylesheet\ncomments checked in November 2026 record a test at 852 by 393 pixels: a block's content\nran to 412, and the blocks butted together. In that layout, padding inside the `100dvh`,\nthrough `box-sizing: border-box`, cost nothing on a tall screen and opened a gap only\nonce the content overflowed.\n\nAt the documented August 2026 quality-gate implementation stage, testing followed the media queries.\n[The site's browser-driven quality gate](/portfolio/a-safety-net-that-no-longer-costs-half-an-hour/)\nwidened its checks to a shared table of twenty-two window shapes, taken from\nevery media query in the stylesheet, and that took the layout check alone from\n95 seconds to 405.\n\nPrint exposed the engines.\n[The print stylesheet](/portfolio/built-a-637-line-print-stylesheet/)\nrecords viewport units resolving to the page box in one engine and to the window\nin another, so a page printed from a wide window lost a third of every line in\nthe second. The masthead's own stylesheet records the media-query side of the\nsame gap: print kept the window's orientation, height and width, so sheets\nprinted from a wide and a narrow window came out with mastheads 47 pixels apart,\nuntil `screen and` at the front of the query confined the rule to windows.\n\nThe containing-block trap is on record as well.\n[The reduced-motion work](/portfolio/made-the-reduced-motion-setting-honest/)\nfound that five different properties each make an element the containing block\nfor anything positioned against the viewport, and that this had silently shrunk\na dismiss layer to a fraction of the screen.\n\n## Where it goes wrong\n\n**The phone shows a tiny desktop page.** Without the viewport `meta` element the\nbrowser lays the page out at a desktop width and scales it down, and the media\nqueries see that width. Add `width=device-width, initial-scale=1`, and leave\nzoom on: `user-scalable=no` takes away the reader's own way to enlarge small\ntext.\n\n**A full-screen section runs past the bottom of the screen.** `100vh` is the\nlarge viewport, so while the address bar is out the bottom of the section, and\nany button placed there, sits behind it. Use `svh` to keep an element clear of expanded browser toolbars, and `dvh` only where resizing mid-scroll is acceptable.\n\n**A full-screen overlay shrinks to a box inside the page.** An ancestor with a\n`transform`, a `will-change` naming one, or certain containment settings can become the containing\nblock for `position: fixed`, and the overlay sizes itself to that ancestor.\nRender overlays outside any transformed element.\n\n**A printed page loses the end of every line.** Viewport units and window-sized\nmedia queries leak into print, and an engine that resolves them against the\nwindow lays the sheet out for a screen. Keep viewport units out of anything that\nprints, and put `screen and` in front of queries meant for windows.\n\n## Further reading\n\n- [CSS Viewport Module Level 1](https://www.w3.org/TR/css-viewport-1/) — the\n  viewport `meta` element and the three `interactive-widget` behaviours.\n- [CSS Values and Units Level 4: viewport-percentage lengths](https://www.w3.org/TR/css-values-4/#viewport-relative-lengths)\n  — the small, large and dynamic viewport sizes and every unit built on them.\n- [CSS Positioned Layout Level 3](https://www.w3.org/TR/css-position-3/) — fixed\n  positioning, the properties that capture it and what it does on paper.\n- [CSS Environment Variables Level 1](https://www.w3.org/TR/css-env-1/) — the\n  safe-area insets and the rule that makes them zero on a rectangular screen.\n- [Media Queries Level 4](https://www.w3.org/TR/mediaqueries-4/) — `width`,\n  `height` and `orientation`, measured on the viewport on screen and on the page\n  box in print.\n\n\u003chttps://engineer.company/glossary/viewport/\u003e\n","date_modified":"2026-11-12T05:34:23Z","description":"How the layout and visual viewports differ, what vh, dvh and lvh measure on a phone, and why fixed overlays and printed pages misbehave.","formats":{"gemtext":"https://engineer.company/glossary/viewport/index.gmi","gopher":"https://engineer.company/glossary/viewport/gophermap","html":"https://engineer.company/glossary/viewport/","json":"https://engineer.company/glossary/viewport/index.json","markdown":"https://engineer.company/glossary/viewport/index.md","pdf":"https://engineer.company/glossary/viewport/index.pdf"},"kind":"page","language":"en","title":"What is a viewport?","url":"https://engineer.company/glossary/viewport/","version":1}