October DealsAmazon USOctober deal check: compare before you payAmazon US: current deals, useful picks and tech finds.Check DealsWindows FixRecommendedWindows errors stealing your time? Find the fix fastScan stability, cleanup and performance issues.Fix NowOctober DealsAmazon USDeal season is back - check today's better picksAmazon US: current deals, useful picks and tech finds.See Picks×
Skip to content
World desk3 min

How to Collect CSS Coverage in Puppeteer

Start CSS coverage before navigation, exercise the states that matter, then inspect returned stylesheet URLs, text, and covered ranges.
Special offer. See more information about Outbyte and uninstall instructions. Please review EULA and Privacy policy.

Start CSS coverage before the navigation or interactions you want to measure, then call page.coverage.stopCSSCoverage() to retrieve stylesheet entries and their used ranges. Exercise relevant page states during the run: coverage describes what that run used, not everything an application might use.

Collect CSS coverage in Puppeteer

This runnable example starts collection before loading the page, exercises an example interaction, then reports covered and total stylesheet text lengths. Replace the example URL and interaction with the page and states you need to measure.

As an Amazon Associate I earn from qualifying purchases.

const browser = await puppeteer.launch();
try {
  const page = await browser.newPage();

  await page.coverage.startCSSCoverage();
  await page.goto('https://example.com');

  // Exercise relevant states, such as menus, tabs, and dialogs.
  // Example: await page.click('[aria-expanded="false"]');

  const cssCoverage = await page.coverage.stopCSSCoverage();

  for (const entry of cssCoverage) {
    const usedBytes = entry.ranges.reduce(
      (sum, range) => sum + range.end - range.start - 1,
      0,
    );
    const totalBytes = entry.text.length;
    const usedPercent = totalBytes
      ? (usedBytes / totalBytes) * 100
      : 0;

    console.log({
      url: entry.url,
      usedBytes,
      totalBytes,
      usedPercent: `${usedPercent.toFixed(1)}%`,
    });
  }
} finally {
  await browser.close();
}

The Puppeteer coverage API documents startCSSCoverage() and stopCSSCoverage(), and its example calculates used bytes by summing end - start - 1 for each range. See the Puppeteer Coverage API and Puppeteer coverage guide.

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

Choose what the run should measure

Start before the relevant events

Call startCSSCoverage() before navigating or interacting. If you begin after a page has loaded, the earlier activity is outside the collection window. Stop only after the page has reached the states whose styles you want included.

Exercise interactions, not just initial load

A page that loads without opening its menu, switching tabs, or showing a dialog cannot establish whether those states use CSS. Include the interactions relevant to your use case before stopping coverage. This is a run-specific measurement, not proof that uncovered styles are universally unused.

Plan around navigation resets

Puppeteer documents resetOnNavigation as true by default. Navigation therefore affects the collection scope. Check the option and configure it deliberately if your run includes navigation and you need coverage behavior across that boundary. The API reference documents the option and its default: Puppeteer Coverage API.

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

Read the coverage entries

Each CSS coverage entry exposes a stylesheet url, its source text, and ranges with start and end positions. Use the URL to identify the stylesheet, the text as the basis for total length, and the ranges to determine what the run covered. See CoverageEntry.

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

The example sums each range’s length using Puppeteer’s documented expression, then divides the result by entry.text.length for a simple percentage. Treat the output as an indicator for this particular run: an unvisited page state may still need the styles that appear unused in the report.

Know what CSS coverage can omit

Puppeteer states that CSS coverage does not include dynamically injected style tags that lack sourceURLs. If an application creates styles at runtime this way, the returned coverage may not represent them. See the caveat in the Puppeteer Coverage API.

Chrome’s Coverage guidance also frames coverage as an activity that begins with a reload and continues while you interact with the page. That supports the practical workflow here: record the states you care about during the run rather than inferring usage from the initial render alone. See Chrome DevTools: Find unused JavaScript and CSS.

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
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 screenshots rather than stylesheet-use ranges, ScreenshotNeo provides a website screenshot API; it does not replace Puppeteer’s CSS coverage API. One GET request returns an image or PDF. For example:

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

See the ScreenshotNeo API documentation. Cookie and consent banners, newsletter popups, and chat widgets are removed before the shot; each of those steps can be turned off. Bot checks, blank pages, timeouts, failed loads, and cache hits are not billed, and response headers report the page verdict and billing status. An MCP server offers take_screenshot, get_page_info, and capture_pdf for AI agents and other MCP clients. The free plan includes 1,000 screenshots per month with no card; paid plans start at $5 for 3,000 shots.

Sign up for ScreenshotNeo’s free plan to try 1,000 screenshots a month with no card.

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
PC Slower Than It Used to Be?Free scan - under a minute
Outdated Drivers Are Slowing You DownFree scan - exact matches

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.