User Agents

Web browsers and everyday web tech, explained

Headless Chrome Fullscreen Window Size Setup Guide

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

To set the window size in headless Chrome, you must pass specific command-line arguments to the browser instance during launch, primarily --window-size=width,height. Because headless mode does not have a physical monitor to reference, Chrome defaults to a small, standard resolution (often 800x600) unless explicitly told otherwise. By defining the window size, you ensure that elements are rendered in their desktop positions, preventing responsive layouts from switching to mobile views and ensuring that screenshots or automated tests capture the entire page content.

What You'll Need

  • A programming environment installed (Node.js, Python, or Java).
  • A browser automation library such as Puppeteer, Playwright, or Selenium.
  • Google Chrome or Chromium installed on the host machine.
  • Basic knowledge of how to pass arguments to a child process or driver configuration.

Step-by-Step

  1. Identify Your Target Resolution: Before writing the code, determine the exact pixel dimensions you need. For a standard 1080p desktop experience, use 1920x1080. For 4K, use 3840x2160. Remember that these dimensions represent the outer window size, not necessarily the inner viewport.
  2. Configure the Launch Arguments: In your automation script, locate the browser launch configuration. You need to add the --window-size flag.

    If using Puppeteer, add it to the args array: await puppeteer.launch({ headless: true, args: ['--window-size=1920,1080'] });

    If using Selenium (Python), add it to ChromeOptions: options = webdriver.ChromeOptions() options.add_argument('--headless') options.add_argument('--window-size=1920,1080')
  3. Set the Viewport (Crucial Step): In many modern libraries, the --window-size flag tells the browser how big the window is, but the internal rendering engine (the viewport) may still be locked to a default size. You must explicitly set the viewport to match the window size.

    In Puppeteer, use: await page.setViewport({ width: 1920, height: 1080 });

    In Playwright, this is typically handled in the browser.newContext() method using the viewport property.
  4. Disable GPU Acceleration (Optional but Recommended): When running in headless mode on Linux servers or CI/CD pipelines, GPU acceleration can sometimes cause rendering glitches or crashes. Add the --disable-gpu flag alongside your window size argument to ensure stability.
  5. Verify the Render: To confirm the settings are working, take a screenshot of the page. If the screenshot is 1920x1080 pixels and the website layout appears as a desktop site rather than a mobile site, the configuration is successful.

Common Mistakes

  • Confusing Window Size with Viewport Size: The most common error is setting --window-size but forgetting to call setViewport. This results in a large window containing a small, centered rendering area, which can lead to incorrect element coordinates during automation.
  • Using Incorrect Syntax: The argument must be exactly --window-size=width,height. Adding spaces around the equals sign or using a semicolon instead of a comma will cause Chrome to ignore the flag and revert to the default size.
  • Ignoring Device Pixel Ratio: On high-DPI screens (Retina displays), the physical pixels and CSS pixels differ. If your screenshots look blurry or twice as large as expected, you may need to adjust the deviceScaleFactor in your viewport settings.
  • Setting Size After Navigation: Changing the window or viewport size after the page has already loaded can sometimes fail to trigger a re-render of responsive elements. Always set the size before calling page.goto().

Tips

  • Match Your Production Environment: If your users primarily use 1366x768, set your headless browser to those exact dimensions to catch layout bugs that only appear at lower resolutions.
  • Use Full-Page Screenshots: If you need to capture the entire length of a page regardless of the height, use the fullPage: true option in your screenshot method rather than trying to set a massive window height (e.g., 10,000px), which can crash the browser.
  • Automate Multiple Sizes: Create an array of common resolutions (Mobile, Tablet, Desktop) and loop your tests through each to ensure full responsive compatibility.

Troubleshooting

If the window size is still not applying, check the following:

  • Check for Conflicting Flags: Ensure you aren't using --start-maximized. In headless mode, --start-maximized often does nothing or conflicts with --window-size because there is no physical screen to maximize to.
  • Verify Chrome Version: Older versions of Chrome had different behaviors regarding headless rendering. Ensure you are using a recent version of Chromium.
  • Inspect the User Agent: Some websites change their layout based on the User Agent string rather than the window size. If the page still looks like a mobile site despite the 1920x1080 setting, manually set a desktop User Agent string in your browser options.
  • Check Memory Limits: Setting an extremely large window size (e.g., 8000x8000) can lead to Out of Memory errors in Docker containers or low-RAM VPS instances.

Frequently Asked Questions

Does --window-size work in non-headless mode?

Yes, but in headed mode, the browser may be overridden by the OS window manager or the --start-maximized flag. It is most reliable in headless mode.

Why is my screenshot still 800x600?

This usually happens because you set the --window-size argument but did not call the library's specific viewport method (like page.setViewport in Puppeteer), which controls the actual rendering area.

Can I change the window size dynamically during a session?

Yes, you can call the viewport resize method at any time during the session to simulate a user resizing their browser window.

Is there a difference between Chromium and Chrome for this setting?

No, the command-line arguments for window sizing are identical for both Chromium and the full Google Chrome browser.

Does the window size affect page load speed?

Indirectly, yes. Larger viewports may trigger the loading of larger images or more complex layout calculations, which can slightly increase the time to reach the networkidle state.