Set the browser viewport before navigation: in PhantomJS assign page.viewportSize = { width: 1024, height: 768 }; in CasperJS pass viewportSize to casper.create() or call casper.viewport(width, height). The viewport controls responsive layout in CSS pixels. It is separate from PhantomJS’s screenshot crop (clipRect) and from PDF paper dimensions (paperSize).
What “screen size” means in these tools
PhantomJS and CasperJS do not change a physical monitor. They emulate a browser window whose viewport has a width and height measured in CSS pixels. A page uses that viewport for media queries, responsive breakpoints, element positioning and JavaScript layout calculations.
Three settings are easy to confuse:
viewportSize: the browser’s layout viewport.clipRect: the rectangle PhantomJS copies into an image.paperSize: the page dimensions used when producing a PDF.
Set the viewport explicitly whenever a repeatable layout or screenshot matters. CasperJS leaves PhantomJS’s documented default of 400 by 300 pixels unchanged unless you configure it.
Set the viewport in PhantomJS
Set it before opening the URL
Assign an object containing numeric width and height before page.open(). This ensures the initial document layout is calculated at the requested size.
#1 Best Overall
var page = require('webpage').create();
page.viewportSize = {
width: 1024,
height: 768
};
page.open('https://example.com/', function (status) {
if (status !== 'success') {
console.log('Open failed: ' + status);
phantom.exit(1);
return;
}
page.render('example-1024x768.png');
phantom.exit();
});
The assignment must be made on the page object, not on the PhantomJS process. Width and height are CSS-pixel dimensions; they are not necessarily the physical pixel dimensions of a high-density display.
Choose dimensions for the responsive breakpoint you need
Use the width that represents the target layout, then choose a height large enough to expose the content you intend to inspect. For example, 375 by 812 approximates a narrow phone layout, 768 by 1024 a tablet portrait layout, and 1440 by 900 a desktop layout. These are viewport choices, not guarantees that a site has a breakpoint at exactly those values.
Render a full page or a visible viewport
page.render() exports according to the filename extension (for example, PNG, JPEG or another supported format). The viewport determines layout, while the captured area depends on PhantomJS rendering settings and any clipping you apply. If a page is taller than the viewport, a normal image render may not represent the entire document; use the page’s full-page rendering behavior or an explicit capture strategy appropriate to your PhantomJS version.
Set the viewport in CasperJS
Configure the initial viewport
Pass viewportSize in the options supplied to casper.create(). This is the most predictable approach when all steps use the same dimensions.
Do these 3 things before closing this tab:
1Repair Windows errors before they cause bigger problems2Fix the driver behind crashes, sound loss and screen glitches3Clear out junk files and repair common Windows errorsvar casper = require('casper').create({
viewportSize: {
width: 1024,
height: 768
}
});
casper.start('https://example.com/');
casper.then(function () {
this.capture('example-1024x768.png');
});
casper.run(function () {
this.exit();
});
Because CasperJS does not override PhantomJS’s 400 by 300 default automatically, omitting this option can produce unexpectedly narrow layouts and screenshots.
Rank #2
- HTML CSS Design and Build Web Sites
- Comes with secure packaging
- It can be a gift option
Change dimensions during a flow
Use casper.viewport(width, height) when one run must test multiple responsive states. When subsequent actions depend on reflow, provide an asynchronous continuation and put the layout-dependent code inside it.
var casper = require('casper').create({
viewportSize: { width: 1024, height: 768 }
});
casper.start('https://example.com/');
casper.then(function () {
this.viewport(375, 812).then(function () {
// The new viewport is effective before this code runs.
this.capture('example-phone.png');
});
});
casper.then(function () {
this.viewport(1440, 900).then(function () {
this.capture('example-desktop.png');
});
});
casper.run(function () {
this.exit();
});
The documented asynchronous form exists to give the page time to reflow. Do not read element positions or click controls immediately after a resize unless the continuation has completed.
Viewport size versus screenshot crop
Changing the viewport does not set the screenshot rectangle. PhantomJS’s clipRect selects the region captured from the page. You can therefore keep a 1024 by 768 layout while capturing only a 400 by 300 area, or capture a larger rectangle when the page supports it.
What’s actually slowing this PC down?
Pick the symptom - the matching free tool is one click away.
var page = require('webpage').create();
page.viewportSize = { width: 1024, height: 768 };
page.open('https://example.com/', function (status) {
if (status !== 'success') {
phantom.exit(1);
return;
}
page.clipRect = {
top: 0,
left: 0,
width: 400,
height: 300
};
page.render('cropped.png');
phantom.exit();
});
Use viewportSize to make the site choose the right responsive layout; use clipRect only when you need a particular output rectangle. A crop cannot make a page reflow to the crop’s dimensions.
PDF dimensions use paperSize
For PDF output, configure print-page dimensions with PhantomJS’s paperSize property. It addresses paper/output pages rather than the browser viewport. See the PhantomJS paper-size API for the supported object forms and margins.
Rank #3
Keep the concerns separate: set viewportSize for CSS layout, paperSize for PDF pages, and (when needed) clipRect for an image crop. A PDF can paginate content even when the viewport is short.
Testing several responsive sizes reliably
- Define a size matrix. Record the exact width and height for each target, rather than relying on a machine’s default.
- Set the initial size before navigation. This prevents the first layout and scripts from running at 400 by 300 or another inherited value.
- Wait for the page state. After navigation, wait for the relevant selector, a known delay, or your normal application-ready condition.
- Resize through Casper’s continuation. When changing size, put measurements, clicks and captures inside the callback supplied to
viewport(). - Keep capture settings explicit. Decide whether you need a viewport image, a crop, a full-page image or a PDF, and configure the corresponding property.
- Save dimensions in filenames and logs. Names such as
home-375x812.pngmake mismatched artifacts easier to diagnose.
Common failures and fixes
The screenshot is 400 by 300
Cause: CasperJS did not override PhantomJS’s default. Fix: add viewportSize to casper.create(), or set PhantomJS’s page.viewportSize before opening the URL.
Quick wins for a faster PC:
Scan for outdated or missing drivers - takes under a minuteDriver Scan →Clear out junk files and repair common Windows errorsFree Scan →The page keeps the old layout after resizing
Cause: layout-dependent code ran before reflow completed. Fix: use casper.viewport(w, h).then(function () { ... }) and perform the capture or measurement inside the continuation.
The image dimensions do not match the viewport
Cause: a clipRect, full-page capture behavior, or output-format setting changed the rendered area. Fix: inspect those settings separately; viewport and crop are independent.
A mobile breakpoint does not activate
Cause: the requested width is not below the site’s breakpoint, or the page uses factors other than width (such as user-agent detection). Fix: verify the actual CSS-pixel width and test the site’s documented breakpoint. Changing height alone will not trigger a width media query.
Rank #4
- 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
The PDF has the wrong page shape
Cause: viewport dimensions were changed instead of print settings. Fix: configure paperSize, including paper format, orientation and margins as required by the PDF job.
Recommended Free Tools
The script fails before the screenshot
Cause: the URL failed to load, the runtime cannot execute a page feature, or the legacy PhantomJS/CasperJS environment is incompatible with the site. Fix: check the status returned by page.open(), log console and page errors where available, and reproduce with a minimal URL. PhantomJS and CasperJS are legacy projects; the API documentation describes behavior but does not establish current maintenance or compatibility with modern sites. Verify your runtime and target pages before adopting them for new automation.
Performance and repeatability considerations
A larger viewport can cause more content, images and client-side work to become visible, increasing capture time and memory use. Full-page output can be substantially heavier than a viewport-sized image. Keep dimensions no larger than the artifact requires, and avoid resizing repeatedly when one fixed viewport is sufficient.
For repeatable comparisons, hold the viewport, user agent, zoom/device scale assumptions, fonts, network conditions and wait strategy constant. A viewport change alone does not emulate every device characteristic. Conversely, a screenshot that looks different after a resize may be the expected result of responsive CSS rather than a rendering error.
Independent reader supportYour contribution helps us test, update, and keep practical guides available for everyone.Or skip the browser setup
If your goal is simply to obtain a dependable website image rather than maintain PhantomJS scripts, ScreenshotNeo provides a one-request screenshot API. The URL is the only target-specific value in this example; the service returns the image bytes.
Best Value
curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp
See the ScreenshotNeo documentation for viewport and capture parameters. Equivalent clients are:
import requests
r = requests.get("https://api.screenshotneo.com/v1/shot", params={"access_key": "YOUR_API_KEY", "url": "https://stripe.com"}, timeout=90)
open("shot.webp", "wb").write(r.content)
const q = new URLSearchParams({ access_key: 'YOUR_API_KEY', url: 'https://stripe.com' });
const res = await fetch(`https://api.screenshotneo.com/v1/shot?${q}`);
Before capture, ScreenshotNeo accepts cookie or consent banners and removes more than 60 known consent platforms, newsletter popups and chat widgets; each cleanup step can be disabled. Bot checks or CAPTCHAs, blank pages, timeouts, failed loads and cache hits are not billed, and response headers identify the page verdict and billing status. It also offers an MCP server with take_screenshot, get_page_info and capture_pdf for Claude, Cursor and other MCP clients. The Free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 shots. Create a free ScreenshotNeo account.
Frequently Asked Questions
Can I set width without setting height?
No. The documented PhantomJS and CasperJS interfaces take both width and height; choose an explicit pair for predictable results.
Does viewport size change device pixel ratio?
No. Viewport dimensions describe CSS pixels. They do not by themselves emulate a phone’s device pixel ratio or every device-specific browser behavior.
PC Slower Than It Used to Be?
A free scan shows the junk files, broken settings and background clutter dragging Windows down - then fixes them in one click.Free scan · Windows 10 & 11Outdated Drivers Are Slowing You Down
One free scan finds every outdated or missing driver and matches the right update for your exact hardware.Free scan · exact hardware matchShould I use viewportSize or clipRect for a thumbnail?
Use viewportSize for the responsive layout you want, then use clipRect if the thumbnail needs a smaller captured rectangle.
Is PhantomJS suitable for a new production test suite?
The documented APIs explain configuration, but the supplied documentation does not establish current maintenance or modern-site compatibility. Validate the runtime against your pages before committing to it.
Quick Recap
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.




