# max-device-width: the media query that reads the real screen

> A binary search over max-device-width finds the real screen whatever screen.width claims. Fixing that route took IPHey from 90 to 100 on one profile.

Source: https://mosaicbrowser.com/blog/device-width-media-query

## screen.width said 1728 and a media query said 1512

A binary search over max-device-width finds the real screen whatever screen.width claims. Fixing that route took IPHey from 90 to 100 on one profile.

![1512: screen.width said 1728. CSS said 1512.](https://mosaicbrowser.com/covers/device-width-media-query.webp)

A page can read the real screen through CSS, whatever `screen.width` says. The `max-device-width` media query answers yes or no for any width you give it, so a binary search over it lands on the exact panel width in about thirteen calls. On one of our profiles `screen.width` said 1728 while that search came back with 1512, the Mac's real panel. Fixing the media-query route moved IPHey from Unreliable 90/100 to Trustworthy 100/100 on the same profile.

## Four lines that find the real screen

The script is short enough to paste into a console:

```
let lo = 1, hi = 8000
while (lo < hi) { const mid = (lo + hi) >> 1
  if (matchMedia(`(max-device-width: ${mid}px)`).matches) hi = mid; else lo = mid + 1 }
lo   // the host's screen, whatever screen.width claims

```

`device-width` is the width of the whole screen, not of the window. The media queries specification has deprecated it in favour of `width`, and browsers still evaluate it. It costs a page nothing to ask. There is no permission prompt and nothing in the network traffic, and the script is optional: a stylesheet with a few `@media (max-device-width: …)` rules can load a different background image per range, and the server learns the bracket from which image was fetched.

On a real browser this tells a site nothing new, because `screen.width` and `device-width` come from the same place. They are one number read two ways. The search only finds something when the two have been made to disagree.

## Why our engine disagreed with itself

Mosaic changes fingerprint values inside Tesseract Chrome, our Chromium, not with injected script. An earlier change in the engine moved `Screen::width()` and its siblings to the profile's claimed screen. That covered `screen.width`, `screen.height` and the avail values, and it stopped there.

The CSS engine does not ask `Screen`. In Blink, `MediaValues::CalculateDeviceWidth` reads `ScreenInfo` directly, a different subsystem that the change never touched. So the profile in question reported this:

```
screen.width                            1728
matchMedia("(device-width: 1512px)")    true    <- the real screen

```

1728 by 1117 is a real MacBook Pro resolution. It was a reasonable thing to claim, and it still failed, because the two routes disagreed.

Mosaic is a desktop app

Email yourself the link and open it on your computer.

[How it works: Machines that exist](https://mosaicbrowser.com/features/real-fingerprints)

## How we found it: one switch of eighteen

We did not find this by reading Blink. IPHey's BROWSER card was red on a profile, and we bisected it the slow way. One profile, changed in place so that nothing else could differ between rounds, with each of the engine's spoofing switches turned off in turn. Of eighteen switches, `--disable-spoofing=screen` was the only one that turned the card green.

That said "the screen", not why. The next rows did:

| Screen the profile claimed | IPHey verdict |
| --- | --- |
| 1728x1117, the baseline | Unreliable, 90/100 |
| screen spoofing off | Trustworthy, 100/100 |
| the host's own 1512x982 | Trustworthy, 100/100 |
| another real Mac, 1470x956 | Unreliable, 90/100 |

The last row is the useful one. A different real Mac resolution failed exactly as the baseline did. If the site were checking claims against a table of real devices, 1470x956 would have passed. It failed because it differed from what CSS reported, so the site was comparing two routes to one value, and any claim other than the true panel would lose.

We ruled out one other suspect on its own. `screen.availTop` was also spoofed, so we set it to the host's real value, 34, and nothing changed.

## The fix: the media query reads the claimed screen

The engine now makes the `device-width` and `device-height` media features read the same claimed screen as `screen.width` and `screen.height`. `device-aspect-ratio` is computed from those two, so it follows with no extra code. The switch that turns screen spoofing off turns this off too, so the control in the table above still means what it says.

One Blink quirk we left alone multiplies the reported screen size into physical pixels on Android. On a platform where it fires, the claimed number should scale with it exactly as the real one would, so the engine passes its value through the same multiply.

Like every other value we change, this one is changed inside the engine, and [why we change values inside the engine and not in JavaScript](https://mosaicbrowser.com/blog/cpp-not-javascript) explains what a script wrapper would have added: something for the page to find.

## Where the second route was hiding

This was one of five values in the engine that turned out to have a second route nobody had looked for. Four of them we found by reading Blink for another caller of the same value. The touch screen was one: `maxTouchPoints` had three more ways to be asked ([the four touch routes](https://mosaicbrowser.com/blog/maxtouchpoints-four-routes)). The screen was the only one a detector found first, because its second caller sat in the CSS engine rather than next to the accessor we had changed.

What changed in how we work is where we search. When a value is claimed, we grep for the value's source, here `ScreenInfo`, across the whole renderer, and stop treating the accessor's neighbours as the full list. A media query, a client hint and a deprecated API are each worth one search before a change is called finished.

## Check your own browser

Run the four lines above, then compare:

```
({ screenWidth: screen.width, cssDeviceWidth: lo })

```

On an ordinary browser the two match. If they differ, something changed `screen.width` and left CSS reading the real screen, and any site that runs the same search sees both numbers. In a Mosaic profile, both come from the profile's one claimed screen, drawn with the rest of its hardware ([how a profile's machine is drawn](https://mosaicbrowser.com/features/real-fingerprints)). The values a page can read, and why a pair that disagrees outweighs either one alone, are in [browser fingerprint](https://mosaicbrowser.com/glossary/browser-fingerprint).

## Questions

### Can a website read my screen size from CSS?

Yes. The device-width and device-height media features describe the whole screen, and a page can test them with matchMedia or in a stylesheet. Searching max-device-width one pixel range at a time returns the exact width in about thirteen checks.

### Is max-device-width deprecated?

The media queries specification deprecates device-width and device-height in favour of width and height, but browsers still evaluate them, so a page can still read them.

### Why would screen.width and the device-width media query disagree?

On a real browser they never do, because both come from the same screen information. They disagree when something has changed screen.width and left the CSS route alone, which is exactly what a consistency check looks for.

### How does Mosaic keep them in agreement?

A change in our engine makes the device-width and device-height media features read the same claimed screen as screen.width and screen.height. device-aspect-ratio is computed from those two, so it follows.

## Related

[A touch screen has four routes, and spoofing one is a contradiction](https://mosaicbrowser.com/blog/maxtouchpoints-four-routes): maxTouchPoints counts touch points; ontouchstart says whether touch events exist. Two media queries also answer, and one changed alone contradicts them.

[Why we change fingerprints in the engine, not in JavaScript](https://mosaicbrowser.com/blog/cpp-not-javascript): A JavaScript spoof is one realm deep. An iframe read the real source of 33 of our replaced functions; the same values set inside the engine agree everywhere.

[Machines that exist: fingerprints drawn from real hardware](https://mosaicbrowser.com/features/real-fingerprints): Mosaic draws each profile's GPU, screen, CPU, memory and fonts from one real device, so the parts of a fingerprint agree with each other and with the host.

[Browser fingerprint: what a site can read about your machine](https://mosaicbrowser.com/glossary/browser-fingerprint): A browser fingerprint is the set of values a site reads about your browser and computer, which together recognize you without cookies. What goes into one.









[How a page checks that a browser function is really native](https://mosaicbrowser.com/blog/native-code-tostring-check): A page calls toString from a fresh iframe to catch a replaced getter, then brand-checks the objects it returns. Neither takes more than four lines.

[Why we built our own Chromium instead of injecting JavaScript](https://mosaicbrowser.com/blog/why-we-built-our-own-chromium): Mosaic changes fingerprint values inside its own engine. Three measured differences between Chrome and plain Chromium explain why that matters.

## Mosaic, on your own computer

The browser this blog is about: its own Chromium, fingerprints from real machines and a proxy IP per profile. Free covers 3 profiles with no card, and Pro is $25 a month for any number of them.

Mosaic is a desktop app

Email yourself the link and open it on your computer.
