To capture a page in dark mode, set prefers-color-scheme to dark before you load the page. That works only if the site has a dark theme: of the nine sites we tested, four changed and five kept the same full-page brightness as in light mode. For those five, the only way to get a dark capture is to have Chrome repaint the page, and the result is the browser's approximation, not the site's design.
This guide gives the code for both methods, the measurements from all nine sites, and the four ways a capture ends up in the wrong theme.
Setting the preference
prefers-color-scheme is the CSS media feature that reports whether a user asked for a light or dark theme, normally through an operating system or browser setting. A headless browser can report either value without touching the system setting.
In Puppeteer, use page.emulateMediaFeatures before goto:
const puppeteer = require('puppeteer');
async function capture(browser, url, scheme, path) {
const context = await browser.createBrowserContext();
const page = await context.newPage();
await page.setViewport({ width: 1280, height: 800 });
await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: scheme }]);
await page.goto(url, { waitUntil: 'load', timeout: 30000 });
await new Promise((resolve) => setTimeout(resolve, 1500));
await page.screenshot({ path });
await context.close();
}
(async () => {
const url = process.argv[2];
const browser = await puppeteer.launch();
await capture(browser, url, 'light', 'light.png');
await capture(browser, url, 'dark', 'dark.png');
await browser.close();
console.log('saved light.png and dark.png for', url);
})();
Run against react.dev, it printed saved light.png and dark.png for https:. Comparing the two files with ImageMagick, about 1,023,700 of the 1,024,000 pixels differed.
In Playwright, set colorScheme on the context:
const { chromium } = require('playwright');
(async () => {
const browser = await chromium.launch();
const context = await browser.newContext({
colorScheme: 'dark',
viewport: { width: 1280, height: 800 },
});
const page = await context.newPage();
await page.goto(process.argv[2], { waitUntil: 'load' });
await page.waitForTimeout(1500);
await page.screenshot({ path: 'dark-pw.png' });
console.log(await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches));
await browser.close();
})();
It printed true. Playwright's docs say colorScheme defaults to 'light', and that null hands the choice back to the system setting. Our test Mac was in light mode, so we couldn't observe what either library does by default on a machine set to dark. Set the value explicitly in both and the question doesn't arise.
What nine sites did
We captured each site three ways at 1280 × 800: light, dark preference, and forced dark (explained below). The brightness columns give the mean grey level of the full-page capture, where 0 is black and 1 is white. "Pixels changed" compares only the first screen of the light and dark-preference captures, counting pixels that differ by more than 3%. We removed the "HeadlessChrome" token from the user agent. A tenth site, Stack Overflow, answered 403 to all three captures, so we left it out.
| Site | Light | Dark preference | Forced dark | Pixels changed, first screen | Dark theme? |
|---|
| react.dev | 0.95 | 0.23 | 0.16 | 100% | Yes |
| tailwindcss.com | 0.85 | 0.09 | 0.11 | 97.2% | Yes |
| example.com | 0.92 | 0.15 | 0.15 | 99.8% | Yes |
| developer.mozilla.org | 0.71 | 0.18 | 0.16 | 5.1% | Yes |
| apple.com | 0.53 | 0.53 | 0.19 | 5.7% | No |
| github.com (signed out) | 0.97 | 0.97 | 0.21 | 0% | No |
| news.ycombinator.com | 0.93 | 0.93 | 0.16 | 0% | No |
| gov.uk | 0.92 | 0.92 | 0.14 | 0% | No |
| bbc.com/news | 0.69 | 0.69 | 0.15 | 0% | No |
Two rows show why a first-screen check can mislead. MDN's homepage opens with a dark hero in both themes, so only 5.1% of that screen changed, yet the full page dropped from 0.71 to 0.18. Apple's first screen also changed by a similar 5.7%, but its full-page brightness stayed at 0.53: there the change was an animated hero caught at a different moment, not a theme. Compare full pages, or a part of the page below the hero.
GitHub's signed-out homepage is a useful warning too. It declares <meta name="color-scheme" content="light dark">, and matchMedia returned true for dark, but the page stayed white because its root element was set to a light theme. A page saying it supports dark doesn't mean you'll get dark.

Forced dark, for sites with no dark theme
Chrome can darken a page itself. The DevTools Protocol, the interface automation tools use to control Chrome, has a method for it, Emulation.setAutoDarkModeOverride, described as rendering all web contents using a dark theme. It's marked experimental, so it may change between versions.
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 });
const cdp = await page.createCDPSession();
await cdp.send('Emulation.setAutoDarkModeOverride', { enabled: true });
await page.goto(process.argv[2], { waitUntil: 'load', timeout: 30000 });
await new Promise((resolve) => setTimeout(resolve, 1500));
await page.screenshot({ path: 'forced.png' });
const canvas = await page.evaluate(() => {
const probe = document.createElement('div');
probe.style.cssText = 'color-scheme: light; background-color: canvas; display: none';
document.body.append(probe);
return getComputedStyle(probe).backgroundColor;
});
console.log('canvas probe', canvas);
console.log('prefers dark', await page.evaluate(() => matchMedia('(prefers-color-scheme: dark)').matches));
await browser.close();
})();
On Hacker News it printed:
canvas probe rgb(18, 18, 18)
prefers dark false
Three things came out of testing it:
- It doesn't set the preference.
prefers-color-scheme: dark stays false, so a site's own dark styles don't run. On react.dev, forced dark gave a different and darker page (0.16) than the site's real dark theme (0.23), as the image at the top of this article shows.
- It keeps photos and strong colours. In our captures the BBC's photos and red banner, Hacker News's orange bar and GitHub's green button all survived.
only light didn't stop it. Chrome's Auto Dark Theme article says <meta name="color-scheme" content="only light"> or color-scheme: only light opts a page out, and the CSS specification says only forbids the browser from overriding the scheme. That article describes an Android origin trial from Chrome 96. In our test, on Chrome 156, a local page with the meta tag and another with the CSS rule both came out with an rgb(18, 18, 18) background. Launching with --enable-features=WebContentsForceDark gave the same result on the meta-tag page. BBC News, which carries the only light meta tag, was darkened as well. Don't count on the opt-out in a capture pipeline.
Use forced dark when you need a dark image and accept that nobody designed it. Label it as such if the capture will be used as evidence of how a site looks, because no real visitor with a dark preference sees that page.
Four ways to get the wrong theme
We built a local test page for each case and read a background pixel from the screenshot. White is 255,255,255 and the dark theme is 17,17,17.
| Case | Result |
|---|
CSS media query, preference set after goto | 17,17,17: works |
Script that reads matchMedia once, preference set after goto | 255,255,255: stays light |
The same script, preference set before goto | 17,17,17: works |
Script that listens for changes, preference set after goto | 17,17,17: works |
0.6 s CSS transition, preference set after goto, captured at once | 246,246,246: nearly white |
| The same transition, 800 ms later | 17,17,17: works |
The same transition, preference set before goto | 17,17,17: works |
Stored theme=light, preference dark | 255,255,255: stays light |
Stored theme=dark, preference light | 17,17,17: stays dark |
Setting the preference too late. Plenty of sites read the preference once, in a script, and add a class. If you change it after load, that script has already run. Playwright behaved the same way: page.emulateMedia({ colorScheme: 'dark' }) after goto left the page white until page.reload().
Catching a transition. Sites that animate between themes can be photographed mid-change if you switch after load. Setting the preference first avoids it, because there's nothing to animate.
A saved choice. A theme stored in localStorage commonly beats the preference. That's also a tool: on react.dev, with the preference set to dark, this script produced a light page, because the site reads a stored theme value:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.setViewport({ width: 1280, height: 800 });
await page.emulateMediaFeatures([{ name: 'prefers-color-scheme', value: 'dark' }]);
await page.goto('https://react.dev/', { waitUntil: 'load' });
console.log('no stored', await page.evaluate(() => document.documentElement.className));
await page.evaluateOnNewDocument(() => localStorage.setItem('theme', 'light'));
await page.goto('https://react.dev/', { waitUntil: 'load' });
console.log('stored light', await page.evaluate(() => document.documentElement.className));
await browser.close();
})();
no stored dark platform-mac
stored light platform-mac
The key name is specific to each site, so inspect its storage first. This also matters when you reuse a saved browser session: if the account or profile has a stored theme, it may override what you set.
Assuming the page has dark at all. Five of our nine sites didn't. If you capture many sites, compare a light and a dark capture of each and treat "no difference" as a normal result, not a failure.
Embedded content
Images and frames follow along. MDN notes that an SVG used through <img> and an <iframe> can respond to prefers-color-scheme, based on the color-scheme of the parent element. On our test page, with the preference emulated as dark, an embedded SVG whose circle has a dark-mode fill turned black and an iframe switched to its dark background. So one setting covers the embedded parts we tried, as long as those documents define a dark style.
Which method to use
- Set
prefers-color-scheme to dark before goto. This is the right answer whenever the site has a dark theme.
- Compare the full page against a light capture, to find out whether it does.
- If the site stores a theme, set that value before the page loads as well.
- If there's no dark theme and you still need a dark image, use forced dark and say that it's a browser rendering.
- Wait for the page to be ready as you would for any capture. Our guide to waiting before a screenshot covers that, and screenshots behind a login covers saved sessions.