User Agents

Web browsers and everyday web tech, explained

Set Puppeteer Browser Window Size Viewport Content Bounds

· 5 min read · Browsers

In this article
  1. Understanding Puppeteer’s Browser Control Model
  2. Setting the Viewport with page.setViewport()
  3. Controlling Full Browser Window Size
  4. Differences Between Viewport and Window Bounds
  5. Common Issues and Limitations
  6. Frequently Asked Questions

To set the browser window size, viewport, content area, and bounds in Puppeteer, use the page.setViewport() method to control the visible viewport dimensions and device scale factor, and browser.newPage() followed by page.setViewport() or page.evaluate() with window resizing for full window control. While Puppeteer primarily automates the visible viewport, advanced configurations using defaultViewport and args like --window-size allow manipulation of the entire browser window.

Understanding Puppeteer’s Browser Control Model

Puppeteer communicates with Chromium via the DevTools Protocol, giving it deep control over page rendering and browser behavior. However, it's important to distinguish between the browser window, the viewport, and the content area. The browser window is the entire application frame, including toolbars and tabs. The viewport is the visible portion of the web page within the browser window. The content area refers to the rendered page content, which can exceed the viewport if scrolling is involved. Puppeteer does not directly manipulate the full browser window by default—it focuses on the viewport to simulate user interactions within the visible area.

By default, Puppeteer launches Chromium with a headless mode and a default viewport of 800x600 pixels unless otherwise specified. This default can be overridden globally via launch options or per-page via methods. The page.setViewport() method is the standard way to set width, height, device scale factor, and whether the viewport is mobile or has touch support. This affects how the page renders responsive content and reports screen dimensions via JavaScript.

Setting the Viewport with page.setViewport()

The primary method for controlling the visible rendering area is page.setViewport(). This method accepts an options object with the following parameters:

  • width: Width of the viewport in CSS pixels
  • height: Height of the viewport in CSS pixels
  • deviceScaleFactor: Emulates high-DPI devices (e.g., retina screens)
  • isMobile: Sets user agent and viewport meta tag behavior for mobile simulation
  • hasTouch: Enables touch event emulation
  • isLandscape: Simulates landscape orientation

Example:

await page.setViewport({
  width: 1920,
  height: 1080,
  deviceScaleFactor: 1,
  isMobile: false
});

This configures the viewport to simulate a desktop monitor at full HD resolution. Note that this does not change the actual browser window size in headful mode—it only changes the area of the page that is rendered and interacted with.

Controlling Full Browser Window Size

To set the actual browser window dimensions (including chrome elements like the address bar), Puppeteer does not offer a direct API method. However, you can influence window size at launch using Chrome command-line arguments. The --window-size flag sets the initial window dimensions when launching the browser.

  1. Launch Puppeteer with custom arguments:
  2. const browser = await puppeteer.launch({
      headless: false, // required for visible window
      defaultViewport: null, // disables default viewport
      args: ['--window-size=1280,800']
    });
    
  3. Open a new page:
  4. const page = await browser.newPage();
    
  5. Optionally set the viewport separately if needed:
  6. await page.setViewport({ width: 1280, height: 720 });
    

Using defaultViewport: null ensures no default viewport is applied, allowing full control. The window size specified in --window-size is interpreted as outerWidth and outerHeight, including browser UI. Note that on some operating systems, the actual size may vary slightly due to OS-level window decorations.

Differences Between Viewport and Window Bounds

A common point of confusion is the distinction between viewport size and window bounds. The viewport is a rendering concept within the page; it determines how CSS media queries respond and how content is laid out. The window bounds refer to the physical dimensions of the browser window on screen. In Puppeteer, page.setViewport() affects only the former.

You can retrieve the actual window dimensions using JavaScript in the browser context:

const windowSize = await page.evaluate(() => {
  return {
    innerWidth: window.innerWidth,
    innerHeight: window.innerHeight,
    outerWidth: window.outerWidth,
    outerHeight: window.outerHeight
  };
});

This returns the real dimensions, which may differ from the viewport if toolbars or devtools are open. In headless mode, outerWidth and outerHeight are not available and return 0, as there is no window frame.

Common Issues and Limitations

Several limitations arise when trying to control window and viewport sizes:

  • Headless vs Headful Behavior: Window size arguments only affect headful mode. In headless mode, there is no physical window, so only viewport dimensions matter.
  • OS-Level Constraints: On some systems, especially macOS with fullscreen optimizations or Windows with multiple monitors, the requested window size may be adjusted by the OS.
  • Viewport Overrides: If defaultViewport is set during launch, it can override page.setViewport() unless explicitly set to null.
  • Responsive Design Testing: Relying solely on viewport size may not fully simulate real devices. Consider using puppeteer.devices presets for more accurate device emulation.

Additionally, setting a large viewport does not guarantee that all content loads or renders correctly. Some websites lazy-load content based on scroll position or network conditions, so viewport size alone doesn’t capture full page behavior.

Frequently Asked Questions

Can Puppeteer resize the browser window after launch?

No, Puppeteer does not provide a direct method to resize the browser window after it has been launched. Window size must be set at launch time using the --window-size argument. Once the browser is running, the window dimensions are fixed unless you manually resize them in headful mode.

Why doesn’t --window-size work in headless mode?

Headless mode runs without a graphical interface, so there is no actual window to size. The --window-size flag only affects headful (visible) browser instances. In headless mode, only the viewport dimensions matter for rendering, controlled via page.setViewport().

How do I set a mobile screen size in Puppeteer?

Use page.setViewport() with mobile dimensions and enable isMobile and hasTouch. For example: { width: 375, height: 667, isMobile: true, hasTouch: true, deviceScaleFactor: 2 }. Alternatively, import a device preset from puppeteer.devices like puppeteer.devices['iPhone 11'] and use page.emulate(device).

Does setting viewport affect screenshot dimensions?

Yes. When you take a screenshot with page.screenshot(), the captured area respects the current viewport size unless you specify fullPage: true or define a custom clip region. The screenshot will match the viewport dimensions by default.

Can I get the actual browser window size in Puppeteer?

You can retrieve inner and outer window dimensions using page.evaluate() to run window.innerWidth, window.innerHeight, window.outerWidth, and window.outerHeight. However, in headless mode, outer dimensions return 0 because there is no window frame.

What is the difference between defaultViewport and setViewport?

defaultViewport is a launch option that sets the initial viewport for all pages unless overridden. page.setViewport() changes the viewport for a specific page after creation. If defaultViewport is set, it applies automatically when a new page is created, but can be changed later with setViewport().