Hardware FixRecommendedDevice not working? Your driver may be the problemCheck updates for common hardware issues.Fix DriversOctober DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsPC HealthRecommendedCrashes, freezes, slowdowns? Check your PC nowSpot repairable issues before they interrupt work.Check PC×
Skip to content
World desk3 min

How to Add Custom CSS to a Page with Puppeteer

Use Puppeteer’s awaited page.addStyleTag() to inject inline CSS or load a stylesheet URL, with frame-specific guidance and a complete example.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Use Puppeteer’s page.addStyleTag() to inject CSS into a page you have already opened. Pass CSS as content for an inline style, or a stylesheet address as url. Await the call before taking a screenshot or relying on the styles.

Inject inline CSS into the current page

After navigating to the target page, call page.addStyleTag() with a content string. Puppeteer inserts a style element into the page’s main frame and returns a handle for that element. The API reference is for Puppeteer 25.12.0: Page.addStyleTag().

As an Amazon Associate I earn from qualifying purchases.

const { launch } = require('puppeteer');

(async () => {
  const browser = await launch();
  try {
    const page = await browser.newPage();
    await page.goto('https://example.com');

    await page.addStyleTag({
      content: 'body { background: papayawhip; }',
    });

    await page.screenshot({ path: 'page.png' });
  } finally {
    await browser.close();
  }
})();

The key sequence is navigation, awaited style injection, then the operation that needs the CSS. Replace the example rule and URL with your own.

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

Load a separate stylesheet by URL

For a stylesheet hosted separately, provide its URL instead of content:

await page.addStyleTag({ url: 'https://example.com/custom.css' });

This adds a stylesheet link to the main frame. Puppeteer documents the two forms as inserting either a <style type="text/css"> element containing CSS or a <link rel="stylesheet"> element for the URL.

Apply CSS inside a child frame

The page-level method targets the main frame. If the element you need to style is inside an iframe, call the corresponding method on that frame rather than assuming the page-level injection will affect it.

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
const frame = page.frames().find((candidate) => candidate.url().includes('widget'));
if (!frame) throw new Error('Target frame not found');

await frame.addStyleTag({
  content: '.target { display: none; }',
});

Adjust the frame selection to match your page. Puppeteer’s Frame.addStyleTag() API provides style injection for a particular frame; the retrieved reference identifies version 25.10.0.

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

Choose the right Puppeteer API

Task Use What it does
Add CSS to an existing page page.addStyleTag({ content: css }) Inserts inline CSS into the main frame.
Load a hosted stylesheet page.addStyleTag({ url: cssUrl }) Inserts a stylesheet link into the main frame.
Style a child frame frame.addStyleTag(...) Injects CSS into that frame.
Run broader DOM logic page.evaluate(fn) Runs a function in the browser page context; Puppeteer awaits a promise returned by the function.
Supply document markup page.setContent(html) Sets the page’s HTML rather than injecting CSS into an already loaded document.

The official references for the alternatives are Page.evaluate() and Page.setContent() (Puppeteer 25.12.0).

Common problems and checks

  • The screenshot has no injected styles: confirm the addStyleTag() call is awaited and runs after navigation but before the screenshot or other dependent action.
  • The target element is in an iframe: locate its frame and use frame.addStyleTag(); page-level injection is documented as a shortcut for the main frame.
  • The external stylesheet does not appear to apply: check the URL and whether the page’s navigation sequence or site-specific policy affects loading. The API reference documents insertion of a link, but cannot guarantee how every target site handles it.
  • You are replacing the page rather than styling it: use page.setContent(html) when the task is to assign document markup. It serves a different purpose from adding a stylesheet to an existing document.
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 only need a screenshot rather than a Puppeteer-controlled browser page, ScreenshotNeo accepts a URL in one GET request. Its API supports custom CSS and JavaScript; its CSS and JavaScript options can be used to adapt the rendered capture. See the API documentation.

curl -G "https://api.screenshotneo.com/v1/shot" -d access_key=YOUR_API_KEY --data-urlencode url=https://stripe.com -o shot.webp

ScreenshotNeo removes known cookie and consent banners, newsletter popups, and chat widgets before capture, with each cleanup step configurable. Bot checks, blank pages, failed loads, timeouts, and cache hits are not billed, and responses identify the page verdict and billing status in headers. An MCP server provides screenshot tools for AI agents, and the free plan includes 1,000 screenshots per month without a card; paid plans start at $5 for 3,000 screenshots.

Create a free ScreenshotNeo account to get 1,000 screenshots a month with no card.

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

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.

Leave a Reply

Your email address will not be published. Required fields are marked *

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

More from the Wire

  1. World desk4 min
    How to Spot an AI Voice Scam Before Sending MoneyDon’t rely on how a caller sounds. Pause, call back through a known number, and verify the emergency with another trusted person before sending money.
  2. Mountain View desk4 min
    Google’s SynthID Detector: How to Check AI-Generated Images, Video and AudioGoogle’s SynthID Detector looks for an embedded watermark in supported images, video and audio. Here is what its results do—and do not—show.
  3. Redmond desk20 min
    How to create a link to File or Folder in Windows 11Windows 11 gives you several ways to point to a file or folder without moving or duplicating it. You can create a desktop shortcut,…
Recommended PC Tool
Recommended PC Tool
Windows Errors? Fix Them Before They SpreadFree repair scan
Crashes, No Sound, or Screen Glitches?Free driver scan

Two free Windows tools

One Free Minute Could Fix That PC

Before you go - each of these free tools takes about a minute and tackles what quietly slows a Windows PC down.

Special offer. View Outbyte info, uninstall instructions, EULA, and Privacy Policy.