Blog

Screen Size Was Never in the User Agent

A persistent misconception is that the user agent conveys screen size. It does not and never has, yet a great deal of layout code depends on inferring it.

The inference runs through a device table

Code that appears to get dimensions from the string is actually identifying a device model and looking up that model's specifications in a dataset.

Two lookups therefore stand between the string and the number, and either can fail. An unrecognised model yields nothing at all.

The chain is invisible to whoever wrote the code, which is why the failure looks like a bug rather than a missing capability.

Physical dimensions are not the useful number

Even a correct hardware specification describes the display, not the space available to the page. Browser interface elements, split screens and window sizing all reduce it.

On systems where a browser window can be resized freely, the display dimensions bear little relationship to the viewport at any moment.

Layout depends on the viewport, so the value the lookup provides is the wrong quantity even when it is accurate.

The viewport is directly available

Both stylesheet media queries and script report the actual space available, updating as it changes. This is a measurement, not an inference.

It handles rotation, window resizing, split-screen use, embedded contexts and devices no dataset has seen, without any rules requiring maintenance.

Anything derived from device identity is at best an approximation of a number the platform already provides exactly.

Pixel density is a separate question

Selecting an appropriately sized image needs the device pixel ratio as well as the viewport, and that is also available directly to both stylesheets and script.

Where the decision must happen at the server, a client hint carries it explicitly, which is a far better source than a device lookup.

Neither route requires knowing which device is in use, which is what makes them robust as new hardware appears.

Where device identity still earns its place

Cases genuinely requiring the model exist: a known hardware defect, support tooling that needs the product name, or hardware capability that cannot be tested.

None of them are layout decisions. Layout has a direct measurement available and should use it.

Separating those two categories removes most device lookups from a codebase, and with them a maintenance burden that never ends.