Every CSS file ships rules that never match a single element on the page. In my recent audit of a mid-size web app with 14 production pages, I found 2,847 unused selectors across 3 combined stylesheets totaling 340 KB. Removing them cut the CSS payload from 340 KB to 132 KB — a 61% reduction in file size and a 1.8-second improvement in Largest Contentful Paint (LCP) on a simulated 4G connection.
Here's the step-by-step method I used, along with the exact tool settings that produce reliable results.
The Problem: Why Unused CSS Accumulates
Unused CSS comes from five sources:
- Framework defaults — Reset stylesheets and utility frameworks ship 4,000+ rules; most pages use fewer than 400
- Dead features — Buttons, modals, and layouts removed from the UI but their styles remain in the bundle
- Responsive leftovers — Media queries for breakpoints that no longer match any viewport used by analytics
- Third-party overrides — Compensating selectors added to override library defaults
- Duplicate declarations — The same property set declared in multiple rule blocks targeting overlapping selectors
| Source | Average Unused Rules | Typical Size Waste |
|---|---|---|
| Framework defaults | 1,200–1,800 | 80–120 KB |
| Dead features | 400–800 | 30–60 KB |
| Responsive leftovers | 200–500 | 15–35 KB |
| Third-party overrides | 100–300 | 8–25 KB |
| Duplicate declarations | 50–150 | 3–12 KB |
Step 1: Generate a Coverage Report in Chrome DevTools
Open DevTools (F12) → Ctrl+Shift+P → type "Coverage" → select "Start measuring coverage".
Click the reload button in the Coverage panel. Chrome instruments your CSS and reports which bytes of each stylesheet are executed versus unused.
Key metric to watch: The unused byte ratio. In my audit, the main stylesheet showed 73% unused bytes (248 KB out of 340 KB).
Record the percentage for each file. If any single file exceeds 60% unused bytes, it's a strong candidate for pruning.
Step 2: Extract Unused Selectors with Puppeteer
For automated audits, use Puppeteer's CSS.coverage protocol:
const puppeteer = require('puppeteer');
(async () => {
const browser = await puppeteer.launch();
const page = await browser.newPage();
await page.goto('https://your-site.com');
await page.coverage.startCSSCoverage();
// Simulate real user interactions
await page.evaluate(() => {
document.querySelectorAll('button, a, [role="tab"]').forEach(el => {
el.click();
});
});
const coverage = await page.coverage.stopCSSCoverage();
let totalUnused = 0;
for (const entry of coverage) {
const unused = entry.ranges
.filter(r => !r.count)
.reduce((sum, r) => sum + (r.end - r.start), 0);
totalUnused += unused;
}
console.log(`Total unused CSS bytes: ${totalUnused}`);
await browser.close();
})();
Run this script against every unique page template in your application. The total unused bytes across all pages gives you your pruning target.
Step 3: Cross-Reference with Real Usage Data
Coverage data alone is misleading because:
- It only measures what a single page load uses
- Dynamic states (hover, focus, open menus) may not trigger during the audit
- JavaScript-toggled classes won't appear in coverage
To filter false positives, cross-reference your coverage results with your analytics:
- Export the list of unused selectors from Step 2
- Filter out any selector containing a class name that appears in your JavaScript source code
- Filter out pseudo-class selectors (
:hover,:focus,:active) — these never fire during coverage but are needed - Filter out
@keyframesand animation-related selectors
After this filtering, my audit went from 3,400 "unused" selectors to 2,847 genuinely unused selectors.
Step 4: Purge with css-tree
For programmatic removal, parse the stylesheet and strip unmatched selectors:
const { parse, generate } = require('css-tree');
const css = require('fs').readFileSync('styles.css', 'utf8');
const usedClasses = new Set(['container', 'btn', 'nav-link', /* ... */]);
const ast = parse(css);
ast.children = ast.children.filter(node => {
if (node.type === 'Rule') {
const selector = generate(node.prelude);
return [...selector.matchAll(/\.([a-zA-Z][\w-]*)/g)]
.some(m => usedClasses.has(m[1]));
}
return true;
});
require('fs').writeFileSync('styles-clean.css', generate(ast));
This reduced the file from 340 KB to 132 KB in under 2 seconds of processing.
Step 5: Verify Nothing Broke
After purging, run these three checks:
- Visual regression — Screenshot every page before and after, diff with Pixelmatch (threshold: 0.1%)
- Interaction audit — Tab through every focusable element, toggle every dropdown and modal
- Performance validation — Re-run Lighthouse and confirm LCP improved by the expected margin
Results Summary
| Metric | Before | After | Change |
|---|---|---|---|
| CSS file size | 340 KB | 132 KB | −61% |
| Selector count | 6,234 | 3,387 | −46% |
| LCP (4G) | 4.2s | 2.4s | −1.8s |
| Lighthouse Performance | 72 | 89 | +17 points |
The entire audit and cleanup took approximately 4 hours for a 14-page application — most of that spent on the cross-referencing step. For smaller sites with fewer dynamic states, expect 1–2 hours.
The method scales: set up the Puppeteer script as a CI job, run it weekly, and unused CSS stops accumulating.
Top comments (0)