Scrapeless Wiki

What Is a Viewport? – Definition and How It Works

Informational P2 what is a viewport

Learn what the viewport is, how layout and visual viewports differ, why device pixel ratio matters, and how viewport size affects automation.

The viewport is the region of a page currently visible in the browser. Its size determines which layout applies, which content renders, and what a screenshot captures.

1. Layout and Visual Viewports

Viewport Meaning Changes with
Layout The area CSS lays out against Window size, orientation
Visual The part actually on screen Pinch zoom, on-screen keyboards

On desktop they usually coincide. On mobile they diverge whenever the user zooms or a keyboard appears, which is why position calculations that ignore the distinction misbehave on phones.

2. Why Size Changes Content

Responsive layouts branch on viewport width, so a narrow viewport can produce a different navigation structure, hide columns, collapse tables, or serve different images.

For collection this is significant: the same URL at two widths can yield genuinely different content, not merely a different arrangement.

3. Device Pixel Ratio

CSS pixels and device pixels are not the same. A high-density screen renders each CSS pixel across several device pixels, so a 1280-wide viewport can produce a 2560-wide screenshot.

Any pipeline mixing measurements with images needs to account for the ratio, or coordinates and image dimensions disagree.

4. Viewport in Automation

Automation libraries let the viewport be set explicitly, and doing so is good practice: it makes runs reproducible and controls which responsive branch is exercised.

Two cautions apply with remote browsers. The provider creates the window, so a requested viewport may not be honoured; and screenshot dimensions can reference a different frame from DOM measurements, which makes coordinate-based clicking unreliable. Addressing elements by reference avoids the whole class of problem.

5. Lazy Loading Depends On It

Content marked for lazy loading only loads when it approaches the viewport. A tall page in a short viewport therefore contains far less than it appears to.

Collecting such a page requires scrolling and waiting, or a taller viewport, or disabling the behaviour β€” but never assuming that a fetched document holds everything a scrolled one would.

6. Choosing a Viewport for Collection

  1. Pick a common desktop size unless mobile content is the target.
  2. Keep it constant across runs so results are comparable.
  3. Record it with the data; it is part of the observation's context.
  4. Use a taller viewport where lazy loading would otherwise require scrolling.
  5. Verify it was applied when using a remote browser rather than assuming.

7. Testing Implications

Because layout branches on width, meaningful testing samples several widths rather than one. Boundaries around common breakpoints are where layout defects concentrate.

8. Summary

The viewport is the visible region of a page, split into layout and visual variants, and its size determines which responsive branch renders and which lazily loaded content exists.

Set it explicitly, keep it constant, record it alongside collected data, and verify it was honoured β€” particularly on remote browsers, where a requested size is a request rather than a guarantee.