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

Use windowWidth and windowHeight in the html2canvas options object when you need the page to render as though it were viewed through a different viewport. Those values influence responsive media queries. They do not, by themselves, determine the bitmap’s pixel dimensions: use width, height, x, y, and scale for the captured canvas and its resolution.

The direct solution

Given an element such as const element = document.querySelector('#invoice');, render it at a 1,200 × 900 CSS-pixel viewport with:

const canvas = await html2canvas(element, {
  windowWidth: 1200,
  windowHeight: 900
});

document.body.appendChild(canvas);

windowWidth and windowHeight are viewport-like dimensions used while html2canvas clones and renders the element. A different width can activate different CSS media queries, change flex or grid wrapping, and alter which responsive navigation or typography rules apply. The values default to the browser’s current window.innerWidth and window.innerHeight.

Viewport size, canvas size, crop, and scale are different controls

Most “wrong size” reports come from solving one sizing problem with an option that controls another. Choose the option according to the result you want.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Goal Options What changes
Reproduce a responsive breakpoint windowWidth, windowHeight The render window used for layout and media-query evaluation.
Set the bitmap’s dimensions width, height The canvas region’s output dimensions; these do not select a responsive breakpoint.
Capture from a particular location x, y The crop origin within the rendered content.
Increase or reduce raster density scale Rendering resolution, independently of the selected viewport layout.

For example, a 375-pixel mobile layout exported at two-times density still uses a 375-pixel responsive viewport, but produces a higher-resolution bitmap. Setting windowWidth: 750 instead would ask responsive CSS to behave like a 750-pixel viewport, which is a different design.

Common implementation patterns

Capture a fixed responsive viewport

async function captureAtViewport(selector, viewport) {
  const element = document.querySelector(selector);
  if (!element) throw new Error(`No element matches ${selector}`);

  return html2canvas(element, {
    windowWidth: viewport.width,
    windowHeight: viewport.height,
    backgroundColor: '#ffffff',
    scale: 1
  });
}

const canvas = await captureAtViewport('#dashboard', {
  width: 1440,
  height: 900
});
const png = canvas.toDataURL('image/png');

This asks for the desktop layout and keeps output density at one device-independent pixel per canvas pixel. Change scale when you need a denser image, not when you need a different breakpoint.

Capture the complete element instead of a clipped viewport

If the element is taller or wider than the visible area, the project’s FAQ recommends using its scroll dimensions as the render-window dimensions:

const element = document.querySelector('#long-report');
if (!element) throw new Error('Report not found');

const canvas = await html2canvas(element, {
  windowWidth: element.scrollWidth,
  windowHeight: element.scrollHeight
});

This is useful when content is clipped or the result is empty, but it is not a guarantee that an arbitrarily large page will fit in one canvas. Browser, operating-system, and hardware canvas limits vary.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #2
Sale
HTML and CSS: Design and Build Websites
  • HTML CSS Design and Build Web Sites
  • Comes with secure packaging
  • It can be a gift option

Control the output rectangle explicitly

const canvas = await html2canvas(element, {
  windowWidth: 1200,
  windowHeight: 900,
  x: 40,
  y: 80,
  width: 800,
  height: 500,
  scale: 2
});

Here the responsive layout is evaluated at 1,200 × 900, while an 800 × 500 region beginning at (40, 80) is rendered at two-times scale. Treat these values as independent; changing one does not implicitly update the others.

A complete browser example with selectable viewports

The following page lets a user choose a breakpoint and download a PNG. It waits for fonts and images that are already available in the document, then passes the selected dimensions to html2canvas.

<select id="viewport">
  <option value="375x812">Mobile 375 × 812</option>
  <option value="768x1024">Tablet 768 × 1024</option>
  <option value="1440x900">Desktop 1440 × 900</option>
</select>
<button id="download">Download PNG</button>
<section id="page-to-capture">…your content…</section>
<script type="module">
  import html2canvas from 'html2canvas';

  const button = document.querySelector('#download');
  const target = document.querySelector('#page-to-capture');

  button.addEventListener('click', async () => {
    const [width, height] = document.querySelector('#viewport').value
      .split('x').map(Number);

    if (document.fonts?.ready) await document.fonts.ready;

    const canvas = await html2canvas(target, {
      windowWidth: width,
      windowHeight: height,
      scale: window.devicePixelRatio || 1,
      useCORS: true
    });

    const link = document.createElement('a');
    link.download = `capture-${width}x${height}.png`;
    link.href = canvas.toDataURL('image/png');
    link.click();
  });
</script>

In a bundler, install html2canvas with your package manager and import it as shown. If you load a browser build directly, make sure the script has finished loading before invoking html2canvas. The useCORS option only helps when image servers send a permission that allows your page’s origin; it cannot override a server’s cross-origin policy.

Choosing dimensions for responsive testing

Match the breakpoint, not the monitor

Use the CSS-pixel width at which you want to test. A phone’s high-density display does not mean you should pass its physical pixel count. For a mobile design, pass a mobile CSS width such as 375 and adjust scale if you need a retina-sized file.

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

Give height enough room for vertical rules

Height can affect media queries and viewport-unit calculations. A short height may trigger a layout that differs from the same width in a tall browser window. Set it to the target device or test fixture rather than relying on the current browser height.

Keep the target element’s geometry in mind

html2canvas reconstructs an image from the DOM and CSS it understands; it does not ask the browser for a native screenshot. Positioning, filters, blend modes, pseudo-elements, and other CSS can therefore differ from what the browser paints. Test the specific components your output depends on.

Why a capture can look clipped, blank, or unexpectedly scaled

Clipped or empty canvas

  • Try the target’s scrollWidth and scrollHeight as windowWidth and windowHeight when you need the whole element.
  • Reduce the requested area or scale if the canvas exceeds a browser or hardware limit. There is no universal maximum that applies to every browser and platform.
  • Check that the target is attached, has non-zero dimensions, and is not hidden by display: none.
  • Capture after asynchronous content, web fonts, and images have loaded.

Layout ignores the requested width

  • Confirm that windowWidth is in the options object passed to the same html2canvas call that produces the canvas.
  • Do not expect width or scale to change media-query selection; they control output geometry and density.
  • Remember that html2canvas renders a cloned DOM representation. CSS that the library does not implement may not respond exactly as it does in a native browser screenshot.

Images are missing or the canvas becomes unusable

For images hosted on another origin, set useCORS: true and configure the image server to permit cross-origin access. Without the server’s permission, browser security can prevent those pixels from being read. Data URLs or same-origin assets avoid this particular dependency.

Fonts, animations, and late content differ

  • Await document.fonts.ready before capture when available.
  • Wait for application data and image load events; starting the render immediately can capture placeholders.
  • Pause animations or add a deterministic state class so the clone is not captured between frames.
  • Use a fixed viewport and scale in automated tests so comparisons are repeatable.

Performance and reliability considerations

Large DOM trees, high scale values, and full-page dimensions increase memory and render time. Capture only the element and rectangle you need, then raise scale gradually. A full-page element can be much larger than the visible viewport; measure its scroll dimensions before choosing a one-canvas export.

Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.
Rank #4
Sale
Web Design with HTML, CSS, JavaScript and jQuery Set
  • Brand: Wiley
  • Set of 2 Volumes
  • A handy two-book set that uniquely combines related technologies Highly visual format and accessible language makes these books highly effective learning tools Perfect for beginning web designers and front-end developers

Because html2canvas depends on browser APIs and runs client-side, it is suited to captures performed in a real page. The project documentation points to Puppeteer or Playwright when you need server-side screenshot generation. Neither approach changes the distinction between responsive viewport dimensions and output bitmap dimensions.

For repeatable output, freeze data, fonts, animations, time-dependent labels, and network-loaded images. Record the requested viewport, canvas dimensions, scale, and browser environment alongside test artifacts; otherwise a visual difference may be caused by rendering conditions rather than your CSS.

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

Or skip the browser setup

If you need a URL screenshot rather than a DOM canvas, ScreenshotNeo returns a PNG, JPEG, WebP, or PDF from one request. It accepts the consent banner like a visitor, then removes more than 60 known consent platforms, newsletter popups, and chat widgets before capture; each cleanup step can be turned off. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads, and cache hits are not billed, and the response identifies the result with X-Page-Verdict and X-Billed headers. It also offers an MCP server for AI agents through take_screenshot, get_page_info, and capture_pdf.

Use the API documentation at https://screenshotneo.com/docs/ for authentication and options. A one-call capture looks like this:

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.
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

Equivalent Python:

import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
r.raise_for_status()
open("shot.webp", "wb").write(r.content)

Equivalent Node.js:

const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
if (!res.ok) throw new Error(`${res.status} ${res.statusText}`);
const data = Buffer.from(await res.arrayBuffer());
await import('node:fs/promises').then(fs => fs.writeFile('shot.webp', data));

The Free plan includes 1,000 screenshots per month with no card. Paid plans start at $5 for 3,000 shots; yearly billing provides two months free, and every feature is included on every plan. Create a free ScreenshotNeo account to start.

When html2canvas is the right tool

  • Use html2canvas when the capture must happen inside the existing page and you need to inspect or manipulate the resulting canvas in browser JavaScript.
  • Use windowWidth/windowHeight to exercise a responsive layout, then independently choose crop, canvas size, and scale.
  • Choose a browser automation or URL screenshot service when you need server-side jobs, pages outside your app, or native browser rendering rather than a DOM reconstruction.

FAQ

Can I set only windowWidth?

Yes. The unspecified height follows html2canvas’s normal default, based on the current window. Set both values when vertical media queries or viewport units matter.

Does windowWidth resize the returned canvas?

No. It selects the render-window dimensions. Set width and height for the output region, and scale for raster density.

Can html2canvas produce a true browser screenshot?

No. It builds an image from the DOM and implemented CSS. The project notes that full CSS support is not possible, so fidelity is not guaranteed for every property.

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

What should I do with extremely long pages?

Try scroll dimensions, then split the page into smaller captures if browser or hardware canvas limits are reached. Treat any maximum as environment-specific rather than universal.

Frequently Asked Questions

Can I set only windowWidth?

Yes. The height then uses html2canvas’s default behavior; set both dimensions when vertical layout rules matter.

Does windowWidth resize the returned canvas?

No. Use width, height, and scale for output geometry and density.

Can html2canvas produce a true browser screenshot?

No. It reconstructs an image from DOM information and supported CSS rather than taking a native browser screenshot.

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

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.