liveBook · Manning

Viewport

As explained in CSS in Depth, Second Edition

A viewport is the framed area of a browser window in which a web page is visible, excluding browser interface controls.

A viewport is the framed area of a browser window where a web page is visible, excluding the address bar, toolbars, and status bar. CSS viewport-relative units size lengths against this area rather than against an element’s font size. section 2.4

The original viewport units translate dimensions into percentages of the viewport:

  • vh and vw represent one percent of the viewport’s height and width. section 2.4
  • vmin and vmax represent one percent of the smaller or larger viewport dimension. section 2.4
  • Large, small, and dynamic families extend these ideas with prefixes l, s, and d, such as lvh, svh, and dvh. section 2.4.1

A square that fits different orientations

Set width: 90vmin and height: 90vmin on an element. Because both dimensions use 90 percent of the smaller viewport dimension, the square remains slightly smaller than the viewport in portrait and landscape orientations. A large hero image can instead use 100vh to match the viewport height, while a masthead can use 100lvh when filling the largest screen matters or 100svh when keeping the entire masthead visible matters more. section 2.4 section 2.4.1

Listing 2.14 Square element sized using vmin
.square {
  width: 90vmin;
  height: 90vmin;
  background-color: #369;
}

Use the listing to inspect the minimal CSS pattern for a viewport-fitting square, then compare its 90vmin dimensions with the rendered result. section 2.4

How CSS in Depth, Second Edition covers it

How deep it goes
Defines original and newer viewport units, explains orientation behavior and trade-offs, and notes scrollbar and keyboard caveats.
Examples
Uses a 90vmin square, a full-height hero image, and lvh-versus-svh masthead sizing.
What it assumes
Builds from em and rem as earlier relative units while defining viewport terminology and basic unit behavior.
What you can do after
Apply viewport-relative units to size responsive elements and choose large or small viewport behavior for mastheads.
How it connects
Connects viewport units with relative units, responsive layout, browser interface elements, scrollbars, and onscreen keyboards.

Also explained in

Compare how 3 books explain viewport →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of CSS in Depth, Second Edition. The section links point to the parts of the book it draws on.