Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Playwright’s page.setViewportSize({ width, height }) before navigating to the page, then call page.screenshot(). Width and height are in CSS pixels. For a viewport shared across pages, configure it on a browser context instead.

Capture a page at a custom viewport

This Playwright Library example opens Chromium, sets a 1280 × 720 CSS-pixel viewport, navigates to the site, and saves the visible viewport as a PNG:

const { chromium } = require('playwright');

(async () => {
  const browser = await chromium.launch();
  try {
    const page = await browser.newPage();
    await page.setViewportSize({ width: 1280, height: 720 });
    await page.goto('https://example.com');
    await page.screenshot({ path: 'screenshot.png' });
  } finally {
    await browser.close();
  }
})();

Install Playwright and its browser before running the script. For example, in a project using the Playwright Library, install the package with npm install playwright and install Chromium with npx playwright install chromium. The viewport method is setViewportSize; it takes an object containing numeric width and height values.

Set the viewport before navigation

Set the viewport before page.goto() when you want the site to load and apply its responsive layout at that size. Playwright’s API documentation notes that changing the viewport can also reset the page’s screen size. If you need to coordinate viewport and screen settings, configure them when creating the browser context instead. See the Playwright Page API.

Free tools Windows power users keep installed

One-click scans. No signup required.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
#1 Best Overall
Sale
Philips 24 Inch Computer Monitor FHD 100Hz VA VESA Flicker-Free, 241V8LB
  • CRISP CLARITY: This 23.8″ Philips V line monitor delivers crisp Full HD 1920x1080 visuals. Enjoy movies, shows and videos with remarkable detail
  • INCREDIBLE CONTRAST: The VA panel produces brighter whites and deeper blacks. You get true-to-life images and more gradients with 16.7 million colors
  • THE PERFECT VIEW: The 178/178 degree extra wide viewing angle prevents the shifting of colors when viewed from an offset angle, so you always get consistent colors
  • WORK SEAMLESSLY: This sleek monitor is virtually bezel-free on three sides, so the screen looks even bigger for the viewer. This minimalistic design also allows for seamless multi-monitor setups that enhance your workflow and boost productivity
  • A BETTER READING EXPERIENCE: For busy office workers, EasyRead mode provides a more paper-like experience for when viewing lengthy documents

Choose where to configure the viewport

Use the configuration scope that matches how broadly the size should apply:

Scope Configuration When to use it
One page await page.setViewportSize({ width: 1280, height: 720 }) Only this page needs the custom size.
Pages in one context browser.newContext({ viewport: { width: 1280, height: 720 } }) Several pages should share the same viewport, or viewport and screen/device settings need to be configured together.
Playwright Test test.use({ viewport: { width: 1280, height: 720 } }) or project use configuration Tests in a test or project configuration should use the same viewport.

Set a context-wide viewport

const context = await browser.newContext({
  viewport: { width: 1280, height: 720 },
});
const page = await context.newPage();
await page.goto('https://example.com');
await page.screenshot({ path: 'context-shot.png' });

Use a context when the same viewport belongs to a group of pages. Context configuration is also the more suitable place to set viewport alongside screen and device-emulation options. See Playwright’s emulation guide.

Set the viewport in Playwright Test

import { test } from '@playwright/test';

test.use({ viewport: { width: 1280, height: 720 } });

test('capture at a custom viewport', async ({ page }) => {
  await page.goto('https://example.com');
  await page.screenshot({ path: 'screenshot.png' });
});

For a project-wide setting, put the same viewport object in that project’s use configuration in the Playwright Test config. A test-level setting is useful when only selected tests need the size.

Rank #2
Philips 22 Inch Computer Monitor FHD 100Hz VA VESA Flicker-Free, 221V8LB
  • CRISP CLARITY: This 22 inch class (21.5″ viewable) Philips V line monitor delivers crisp Full HD 1920x1080 visuals. Enjoy movies, shows and videos with remarkable detail
  • 100HZ FAST REFRESH RATE: 100Hz brings your favorite movies and video games to life. Stream, binge, and play effortlessly
  • SMOOTH ACTION WITH ADAPTIVE-SYNC: Adaptive-Sync technology ensures fluid action sequences and rapid response time. Every frame will be rendered smoothly with crystal clarity and without stutter
  • INCREDIBLE CONTRAST: The VA panel produces brighter whites and deeper blacks. You get true-to-life images and more gradients with 16.7 million colors
  • THE PERFECT VIEW: The 178/178 degree extra wide viewing angle prevents the shifting of colors when viewed from an offset angle, so you always get consistent colors

Combine a device preset with a custom viewport

When using a device descriptor, spread it first and set your custom viewport afterward so the explicit dimensions override the descriptor’s viewport. Add deviceScaleFactor if the intended device pixel ratio matters. A custom viewport alone changes the layout dimensions; it does not necessarily reproduce every characteristic of a physical device. See the emulation documentation.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
const { devices, chromium } = require('playwright');

const browser = await chromium.launch();
const context = await browser.newContext({
  ...devices['iPhone 13'],
  viewport: { width: 390, height: 844 },
  deviceScaleFactor: 2,
});
const page = await context.newPage();

Choose what part of the page to capture

Viewport configuration controls the browser’s visible layout area. Screenshot options control the captured area:

  • page.screenshot() with no extent option captures the currently visible viewport.
  • fullPage: true captures the full scrollable page.
  • clip: { x, y, width, height } captures a specific rectangle.
// Capture the visible viewport
await page.screenshot({ path: 'viewport.png' });

// Capture the full scrollable page
await page.screenshot({ path: 'full-page.png', fullPage: true });

// Capture a rectangle, in page CSS pixels
await page.screenshot({
  path: 'region.png',
  clip: { x: 0, y: 0, width: 640, height: 360 },
});

Full-page capture is not the same as making the viewport taller: it captures beyond the visible viewport. Use a custom viewport to control responsive layout; use fullPage when the goal is the entire scrollable document. For option details, see the Playwright screenshot API.

Rank #3
Sale
Dell 24 Monitor - SE2426H - 23.8-inch FHD (1920x1080) 144Hz 1ms Display, in-Plane Switching (IPS) Technology, AMD FreeSync™, TÜV 3-Star 2X HDMI, Tilt
  • Clear visuals. Fluid motion: A 144Hz refresh rate and 1ms MPRT deliver smooth, tear‑free motion across work, gaming, and streaming for clearer, more fluid viewing.
  • Eye comfort: TÜV Rheinland 3‑star* certification reduces harmful blue light while preserving stunning color quality without compromise. *TÜV Rheinland 3-star eye comfort certification.
  • Wide viewing angle: Get consistent views across a wide 178° /178° viewing angle.
  • In-Plane Switching (IPS): See excellent color accuracy and consistency across wide viewing angles with In-plane Switching (IPS) technology.
  • Ultra-thin bezels: Maximize your viewing experience with thin bezels.

Choose screenshot format and pixel scale

The screenshot API supports PNG, JPEG and WebP output. Set the file extension or specify the desired type; JPEG and WebP support a quality option. Transparent backgrounds are supported for formats that support transparency, such as PNG.

await page.screenshot({ path: 'shot.webp', type: 'webp', quality: 85 });

Scale determines the relationship between CSS pixels and output pixels:

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
  • scale: 'css' produces one image pixel per CSS pixel.
  • scale: 'device' produces device pixels. At a higher device scale factor, the image can be substantially larger.
await page.screenshot({ path: 'css-pixels.png', scale: 'css' });
await page.screenshot({ path: 'device-pixels.png', scale: 'device' });

Choose CSS scale for output dimensions that track the CSS layout. Choose device scale when you need the higher-DPI rendering. The screenshot API reference lists the supported options.

Rank #4
Sale
Samsung 27" Essential S3 (S36GD) Series FHD 1800R Curved Computer Monitor
  • CURVED FOR ENHANCED ENGAGEMENT: An immersive viewing experience with a curved monitor that wraps more closely around your field of vision; It creates a wider view, enhancing depth perception and minimizing peripheral distraction
  • SMOOTH PERFORMANCE FOR SEAMLESS CONTENT: Stay in the action when playing games, watching videos, or working on creative projects; The 100Hz refresh rate reduces lag and motion blur so you don't miss a thing in fast-paced moments¹
  • MORE GAMING POWER: Gain the edge with optimizable game settings; Color and image contrast can be adjusted to see scenes more vividly and spot enemies hiding in the dark; Game Mode adjusts any game to fill the screen so you can view every detail²
  • KEEP IT EASY ON THE EYES: Care for your eyes and stay comfortable, even during long sessions; Advanced eye comfort technology certified by TÜV reduces eye strain by minimizing blue light and reducing irritating screen flicker²
  • INCREASED VERSATILITY: Connect to more; Plug devices straight into your monitor for increased flexibility, making your computing environment even more convenient

Make visual comparisons more reliable

A fixed viewport does not guarantee identical pixels on different machines. Browser version, operating system, browser settings, hardware conditions and headless mode can all affect rendering. For visual regression comparisons, keep those conditions consistent as well as the viewport. Playwright explains this in its visual comparison guidance.

Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Support on Ko-Fi

Troubleshoot common problems

The screenshot has the wrong dimensions

  • Confirm that both viewport values are numbers in CSS pixels and that the call happens before navigation.
  • Check whether a later viewport setting or device descriptor overwrites the dimensions. When combining a device preset with custom dimensions, put the custom viewport after the spread descriptor.
  • Distinguish viewport size from output pixel size: scale: 'device' can produce more image pixels than CSS pixels.

The screenshot shows only the top of a long page

A normal screenshot captures the visible viewport. Add fullPage: true to capture the scrollable document, or use clip for a particular rectangle.

The site uses its desktop layout despite a narrow viewport

Set the viewport before navigating so the page initializes at the intended dimensions. A viewport is not a complete phone simulation; if the page depends on device characteristics, configure an appropriate device descriptor and, where needed, its device scale factor.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Best Value
Sale
Sceptre New 22-Inch Gaming Monitor, FHD 1080p, Up to 144Hz, HDMI, DisplayPort, Built-in Speakers, Machine Black (E225W-FW144 Series, 2026)
  • 【INTEGRATED SPEAKERS】Whether you're at work or in the midst of an intense gaming session, our built-in speakers provide rich and seamless audio, all while keeping your desk clutter-free.
  • 【EASY ON THE EYES】 Protect your eyes and enhance your comfort with Blue-Light Shift technology. This feature reduces harmful blue light emissions from your screen, helping to alleviate eye strain during long hours of use and promoting healthier viewing habits.
  • 【WIDEN YOUR PERSPECTIVE】Our sleek minimal bezel design ensures undivided attention. The nearly bezel-free display seamlessly connects in a dual monitor arrangement, delivering an unobstructed view that lets you focus on more at once, completely distraction-free.

Screenshots differ between runs or machines

Use the same browser version, operating system, headless mode, settings and hardware conditions for comparisons. A matching viewport alone cannot eliminate rendering differences.

The browser does not launch

Install the browser binary for the Playwright package in use. For a Chromium-only setup, the example command is npx playwright install chromium. If the project uses Playwright Test, make sure the package and browser installation match that project’s setup.

Or skip the browser setup

If you need an image or PDF from a URL without managing a local browser, ScreenshotNeo provides a screenshot API and MCP server. Its API accepts a URL in one GET request; see the API documentation for options.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://example.com -o shot.webp
  • Cookie and consent banners, newsletter popups and chat widgets are removed before capture; each cleanup step can be turned off.
  • Bot checks or CAPTCHAs, blank pages, timeouts, failed loads and cache hits are not billed. Responses identify the page verdict and billing status in headers.
  • An MCP server gives AI agents tools for taking screenshots, getting page information and capturing PDFs.
  • The free plan includes 1,000 screenshots a month without a card; paid plans start at $5 for 3,000 screenshots.

Sign up for ScreenshotNeo’s free plan to try it without a card.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Frequently Asked Questions

Can I save a Playwright screenshot as a buffer instead of a file?

Yes. Call page.screenshot() without a path; it returns a buffer you can pass to image-processing code.

Does setViewportSize() use device pixels?

No. Its width and height describe the viewport in CSS pixels. Screenshot output pixel dimensions are affected separately by the screenshot scale and device scale factor.

Product prices and availability are accurate as of the date/time indicated and are subject to change. Any price and availability information displayed on Amazon at the time of purchase will apply.