Show Scrollbars in Chrome Headless Puppeteer Screenshots
In this article
To show scrollbars in Chrome headless Puppeteer screenshots, you must force the browser to render the scrollbar by ensuring the viewport is smaller than the content and using CSS to override the default headless behavior that often hides overflow indicators. By default, Puppeteer in headless mode often treats the page as a canvas and removes the visual scrollbar gutters to provide a clean image. To bring them back, you need to inject a specific CSS style into the page that forces the scrollbar to be visible and ensures the fullPage option is handled carefully, as taking a full-page screenshot often removes the need for a scrollbar entirely since the entire document is captured in one long image.
What You'll Need
- Node.js installed on your system.
- The
puppeteerlibrary installed via npm. - A target URL or local HTML file that contains enough content to trigger a vertical or horizontal overflow.
- Basic knowledge of JavaScript and CSS selectors.
Step-by-Step
- Initialize Puppeteer: Start by launching the browser in headless mode. While
headless: trueis the default, ensure you are using the "new" headless mode (headless: 'new'in older versions or simplytruein newer ones) for the most accurate rendering of Chrome's modern engine. - Set a Fixed Viewport: You must set a viewport size that is smaller than the actual content of the page. If the viewport is set to the size of the full page, no scrollbars will appear because there is nothing to scroll. Use
page.setViewport({ width: 1280, height: 720 })to simulate a standard desktop window. - Navigate to the Page: Use
page.goto(url, { waitUntil: 'networkidle2' }). It is critical to wait for the network to be idle to ensure that all styles and dynamic content have loaded, as layout shifts can happen after the initial load, potentially removing the need for a scrollbar. - Inject Custom CSS: This is the most important step. Headless Chrome often hides the scrollbar track. You need to use
page.addStyleTagto force the scrollbar to be visible. Add a style that targets thehtmlandbodyelements. Use the following CSS:::-webkit-scrollbar { width: 16px !important; height: 16px !important; display: block !important; }and::-webkit-scrollbar-thumb { background: #888 !important; border-radius: 10px !important; }. This forces the WebKit engine to render a visible bar with a specific color. - Force Overflow: Ensure the body has
overflow: scroll !important;rather thanoverflow: auto;. Whileautoonly shows the bar when needed,scrollforces the gutter to be present regardless of the content length, which is often necessary for consistent screenshots. - Capture the Screenshot: Call
page.screenshot({ path: 'screenshot.png', fullPage: false }). It is vital thatfullPageis set tofalse. If you set it totrue, Puppeteer expands the viewport to the height of the entire document, which eliminates the scrollbar because the "window" is now as large as the content. - Verify the Output: Open the resulting PNG file to ensure the gray scrollbar thumb and track are visible on the right-hand side of the image.
Common Mistakes
The most frequent error is enabling the fullPage: true option in the screenshot configuration. Many developers assume that to see the whole page and the scrollbar, they need this setting. However, fullPage: true effectively tells Chrome to render the page as one giant image. In a giant image, there is no "window" to scroll through, so the browser removes the scrollbar entirely. To see the scrollbar, you must capture only the visible viewport.
Another common mistake is forgetting to set the waitUntil property. If the screenshot is taken before the CSS is fully applied or before the content has expanded to exceed the viewport height, the scrollbar will not be triggered. This is especially common in Single Page Applications (SPAs) where the content is rendered via JavaScript after the initial HTML load.
Some users attempt to use page.evaluate() to change styles, but page.addStyleTag() is more reliable for forcing global WebKit scrollbar styles. Using evaluate to change the body.style.overflow may work, but it won't style the scrollbar's appearance, leaving it as a thin, nearly invisible line in headless mode.
Tips
To make the scrollbar highly visible for documentation or bug reports, use high-contrast colors in your injected CSS. Instead of a subtle gray, use background: red !important; for the ::-webkit-scrollbar-thumb. This ensures that the scrollbar is unmistakable in the final image.
If you are testing a site that uses a custom scrollbar library (like OverlayScrollbars or SimpleBar), the standard WebKit CSS overrides might not work. In these cases, you will need to inspect the DOM to find the specific class names used by the library (e.g., .scrollbar-thumb) and force those elements to be visible and opaque using page.addStyleTag.
For cross-browser testing simulation, remember that headless Chrome uses the WebKit engine. If you are trying to mimic how Firefox or Safari handles scrollbars, the CSS selectors will differ. For Firefox, you would need to use scrollbar-width: thin; and scrollbar-color: #888 #eee;.
Troubleshooting
If the scrollbar is still not appearing, first check if the page has a CSS rule like overflow: hidden; on the body or html tags. Some modern websites hide the default scrollbar and implement a custom one. Use page.addStyleTag({ content: 'html, body { overflow: scroll !important; }' }) to override these restrictions.
If the screenshot looks correct but the scrollbar is missing, check the viewport dimensions. Use await page.evaluate(() => document.documentElement.scrollHeight) to log the actual height of the page and compare it to the height you set in setViewport. If the page height is less than or equal to the viewport height, the scrollbar will never appear. You may need to reduce the viewport height to 600px or 400px to force the overflow.
In some versions of Puppeteer, the headless: true mode behaves differently than headless: 'new'. If you are using an older version of Puppeteer, try switching to the new headless mode, which is based on the actual Chrome browser rather than a stripped-down implementation. This ensures that the rendering engine handles scrollbars more like a standard user browser.
Frequently Asked Questions
Why does fullPage: true hide my scrollbars?
When fullPage is true, Puppeteer expands the browser's viewport to match the total height of the document. Since the viewport is now the same size as the content, there is no overflow, and the browser removes the scrollbar because it is no longer needed for navigation.
Can I change the color of the scrollbar in the screenshot?
Yes, by using the ::-webkit-scrollbar-thumb and ::-webkit-scrollbar-track pseudo-elements in an injected style tag, you can set any background color or border you wish.
Do these steps work for horizontal scrollbars?
Yes. To show a horizontal scrollbar, ensure the viewport width is smaller than the content width and use the ::-webkit-scrollbar height property in your CSS to ensure the horizontal track is rendered.
Will this work on a Linux server without a GUI?
Yes, Puppeteer is designed to run on headless Linux servers. As long as the necessary Chrome dependencies are installed on the OS, the CSS injection method will work regardless of whether a physical monitor is attached.
Does the scrollbar appear in the screenshot if the page is short?
Not by default. However, if you set overflow: scroll !important; on the body, Chrome will render the scrollbar gutter even if the content doesn't require scrolling, though the "thumb" (the draggable part) may be the full height of the bar.