Headless Chrome Won't Render Narrower Than 500px
Nov 16, 2026
If you've ever taken a 390px screenshot with headless Chrome to check your mobile layout, there's a decent chance you were looking at your 500px layout instead.
The quick way to screenshot a page at a given size looks like this:
chrome --headless --window-size=390,900 --screenshot=shot.png https://example.comYou get back a PNG that's exactly 390 by 900. It looks like a phone screenshot, but the page inside it wasn't laid out at phone width.
Seeing It for Yourself
Save this as probe.html. It prints the width the page actually laid out at:
<!doctype html>
<meta name="viewport" content="width=device-width,initial-scale=1">
<pre id="o" style="font-size:40px"></pre>
<script>
o.textContent =
`innerWidth ${innerWidth}\n` +
`clientWidth ${document.documentElement.clientWidth}\n` +
`(width:390px) ${matchMedia('(width: 390px)').matches}`;
</script>Then screenshot it at 390:
chrome --headless --window-size=390,900 --screenshot=shot.png file://$PWD/probe.htmlOn Chrome 157 the image is 390 wide, and it reads:
innerWidth 500
clientWidth 500
(width:390px) falseThe page was laid out at 500px and then cropped to 390. I tried a handful of sizes and everything under 500 came back as 500:
asked 320 -> clientWidth 500
asked 390 -> clientWidth 500
asked 430 -> clientWidth 500
asked 499 -> clientWidth 500
asked 768 -> clientWidth 768
asked 1440 -> clientWidth 1440(The number varies. My notes from August, on a different Chrome version, have a 390 request rendering at around 790. Either way, not 390.)
Why a 500px Render Breaks Mobile Testing
Any CSS that kicks in below 500px, which is most of your phone layout, never runs. Your 480px breakpoint is skipped, your hamburger menu might not appear, and the overflow bug that only happens at 390 doesn't happen. The cropped screenshot then hides the right side of the page, so you can't even tell it's a wider layout unless you look closely.
Getting a Real Phone-Width Render
Set the viewport through the DevTools protocol instead of the window size. Playwright does that for you:
import { chromium } from 'playwright';
const browser = await chromium.launch();
const page = await browser.newPage({ viewport: { width: 390, height: 900 } });
await page.goto('https://example.com');
const ok = await page.evaluate(() => matchMedia('(width: 390px)').matches);
if (!ok) throw new Error('Viewport is not 390px');
await page.screenshot({ path: 'shot.png' });
await browser.close();The matchMedia check asks CSS what width it thinks the viewport is, which is the only number your media queries care about. If a tool ever quietly gives you a different size, the script fails loudly instead of handing you a screenshot that looks like evidence.



Comments
Leave a Comment