Fixing the Lighthouse Reduce unused CSS warning with PurgeCSS
Lighthouse's "Reduce unused CSS" warning goes away when you replace the CSS file with one that PurgeCSS has trimmed to remove the rules the page does not use. The warning appears on pages that load a whole CSS framework such as Bootstrap. Use this fix when you want a page to render faster or reduce the amount of data transferred. On the page I tested, the CSS transfer size dropped from 31,063 bytes to 3,501 bytes. However, depending on how you remove the rules, classes that JavaScript adds later can lose their styling. I ran Lighthouse and PurgeCSS in a test environment and show the results and numbers exactly as they came out.
I tested with Ubuntu 24.04, Node.js 22, Lighthouse 13.6.0 (the npm package), PurgeCSS 7.0.2, Bootstrap 5.3.9, and Playwright 1.63.0. Chrome is Chrome for Testing 158. I ran Lighthouse from the command line with the default settings (mobile), checked only the performance category, and ran each page three times. I served the pages with gzip from a small server in the test environment (localhost:8080). I did not test the Lighthouse panel in Chrome DevTools, PageSpeed Insights, desktop settings, a live public site, Tailwind CSS, or using PurgeCSS integrated with PostCSS, webpack, or Vite.
The warning appeared on a page that loads all of Bootstrap
The test page has only a heading, a description, one card, and a button labeled Save record. The CSS is Bootstrap's bootstrap.min.css (232,360 bytes), loaded as-is under the name style.css. The following HTML shows the end of <head> and the contents of <body>.
<link rel="stylesheet" href="./style.css">
</head>
<body>
<main class="container py-4">
<h1 class="h3 text-primary">猫の健康メモ</h1>
<p class="lead">毎日の体重とごはんの量を記録するページです。</p>
<div class="card mb-3">
<div class="card-body">
<h2 class="card-title h5">今日の記録</h2>
<p class="card-text">体重 4.2kg、ごはん 60g。</p>
<button id="save" type="button" class="btn btn-primary">記録を保存</button>
</div>
</div>
<div id="message"></div>
</main>
<script src="./main.js" defer></script>
When you press the button, JavaScript creates a notification box and displays it with the Bootstrap classes alert and alert-success. The class name is built by joining 'alert-' and the variable kind.
// When the save button is clicked, show the result in an alert box
const kind = 'success';
document.getElementById('save').addEventListener('click', () => {
const box = document.createElement('div');
box.className = 'alert alert-' + kind;
box.textContent = '記録を保存しました';
document.getElementById('message').append(box);
});
I ran Lighthouse on this page. Because of --quiet, Lighthouse itself prints nothing. A small script (summarize.mjs) extracts the score, the metrics, and the result of this audit from the JSON output. In this article I leave out the line for another audit (render-blocking requests) from the summarize.mjs output.
$ npx lighthouse http://localhost:8080/before/ --only-categories=performance --output=json --output=html --output-path=./out/before-1 --chrome-flags="--headless=new --no-sandbox" --quiet
$ node summarize.mjs out/before-1.report.json
lighthouseVersion: 13.6.0 requestedUrl: http://localhost:8080/before/
performance score: 100
FCP: 0.9 s (score 1) LCP: 0.9 s (score 1) SI: 0.9 s (score 1) TBT: 0 ms (score 1) CLS: 0 (score 1)
unused-css-rules: score=0 scoreDisplayMode=metricSavings title="Reduce unused CSS" displayValue="Est savings of 29 KiB" metricSavings={"FCP":150,"LCP":150}
- http://localhost:8080/before/style.css total=30859 wasted=29722 (96.3%)
stylesheets loaded: /before/style.css(31063B)
The result shows that 96.3% of style.css is not used when the page is opened. With the Japanese locale (--locale=ja), the audit title and the estimate were shown in Japanese, meaning "Reduce unused CSS" and "Estimated savings of 29 KiB." All three runs gave the same result, and the expected FCP and LCP improvement (metricSavings) was 150 ms.

The performance score was 100 in all three runs. As the "Unscored" label on the audit shows, this audit does not count toward the score. The weight of this audit (unused-css-rules) is also 0 in the default config file of Lighthouse 13.6.0.
Keeping only the classes that appear in the HTML with PurgeCSS
PurgeCSS reads files such as HTML and JavaScript (called content) and removes CSS selectors that do not appear in them. First, I specified only index.html as content. --output specifies the name of the file to write.
$ npx purgecss --css node_modules/bootstrap/dist/css/bootstrap.min.css --content src/index.html --output out/purge-html-only.css
The output file was 10,761 bytes (3,159 bytes after gzip), a large drop from the original 232,360 bytes. I served it as style.css and ran Lighthouse again. The warning went away. All three runs were the same.
$ npx lighthouse http://localhost:8080/html-only/ --only-categories=performance --output=json --output=html --output-path=./out/html-only-1 --chrome-flags="--headless=new --no-sandbox" --quiet
$ node summarize.mjs out/html-only-1.report.json
lighthouseVersion: 13.6.0 requestedUrl: http://localhost:8080/html-only/
performance score: 100
FCP: 0.8 s (score 1) LCP: 0.8 s (score 1) SI: 0.8 s (score 1) TBT: 0 ms (score 1) CLS: 0 (score 1)
unused-css-rules: score=1 scoreDisplayMode=metricSavings title="Reduce unused CSS" displayValue="" metricSavings={"FCP":0,"LCP":0}
stylesheets loaded: /html-only/style.css(3374B)
However, when I pressed the button on this page, the notification box did not appear. The Japanese text meaning "Record saved" appeared, but it was plain text with no green background and no border.

I pressed the button with Playwright and checked the style of the box that appeared (the values the browser computed). On the page before the fix (before), the background is a light green, the inner padding is 16px, and the border is 1px. On the page where I trimmed the CSS using only the HTML (html-only), the background is transparent, and the padding and border are 0.
$ node check.cjs
before: class="alert alert-success" background=rgb(209, 231, 221) color=rgb(10, 54, 34) padding=16px border=1px solid radius=6px
html-only: class="alert alert-success" background=rgba(0, 0, 0, 0) color=rgb(33, 37, 41) padding=0px border=0px none radius=0px
(The actual output also shows the button's background color at the end of each line. The check.cjs output in this article leaves that part out.)
alert and alert-success are not written in the HTML. They are classes that JavaScript adds when the button is pressed. When I checked the trimmed CSS, neither the .alert{ rule nor the .alert-success{ rule remained. Even so, as shown above, Lighthouse did not raise the warning. If the appearance breaks after a button press, the Lighthouse result does not show it.
Adding the JavaScript file to content too
I added main.js to the content and trimmed again.
$ npx purgecss --css node_modules/bootstrap/dist/css/bootstrap.min.css --content src/index.html src/main.js --output out/purge-html-js.css
This time the .alert{ rule remained, and the padding and border came back. However, the background is still transparent, because the .alert-success{ rule was not kept. Here is the result from check.cjs.
html-js: class="alert alert-success" background=rgba(0, 0, 0, 0) color=rgb(33, 37, 41) padding=16px border=1px solid radius=6px
The cause is that the class name is built by joining strings: 'alert-' + kind. The safelist section of the PurgeCSS documentation says that PurgeCSS does not run code. It reads the files in content as strings and keeps selectors whose names appear in them. A class name built by joining strings cannot be found, so its CSS is removed. The .alert-success{ rule was removed on this page because the string alert-success does not appear anywhere in main.js.
Two ways to keep class names
The same documentation gives two fixes. One is to write the full class names in the content files. The other is to add them to the safelist so they are not removed. I tried both.
Adding them to the safelist
On the PurgeCSS command line, --safelist takes the names of the classes you want to keep, so they are not removed.
$ npx purgecss --css node_modules/bootstrap/dist/css/bootstrap.min.css --content src/index.html src/main.js --safelist alert-success --output out/purge-safelist.css
The output file was 11,568 bytes (3,286 bytes after gzip), and both the .alert{ and .alert-success{ rules remained. When I checked it with check.cjs, the box that appeared after the button press had the same style as before the fix.
safelist: class="alert alert-success" background=rgb(209, 231, 221) color=rgb(10, 54, 34) padding=16px border=1px solid radius=6px

Lighthouse raised no warning in any of the three runs. The CSS transfer size was 3,501 bytes, about one-ninth of the 31,063 bytes before the fix. FCP and LCP went from 0.9 s to 0.8 s. The score was 100, the same as before.
$ npx lighthouse http://localhost:8080/safelist/ --only-categories=performance --output=json --output=html --output-path=./out/safelist-1 --chrome-flags="--headless=new --no-sandbox" --quiet
$ node summarize.mjs out/safelist-1.report.json
lighthouseVersion: 13.6.0 requestedUrl: http://localhost:8080/safelist/
performance score: 100
FCP: 0.8 s (score 1) LCP: 0.8 s (score 1) SI: 0.8 s (score 1) TBT: 10 ms (score 1) CLS: 0 (score 1)
unused-css-rules: score=1 scoreDisplayMode=metricSavings title="Reduce unused CSS" displayValue="" metricSavings={"FCP":0,"LCP":0}
stylesheets loaded: /safelist/style.css(3501B)

Writing the full class names
This method changes the JavaScript. I prepared a lookup table that writes out the full class name for each kind value.
// When the save button is clicked, show the result in an alert box (full class names written out)
const kinds = { success: 'alert-success', danger: 'alert-danger' };
const kind = 'success';
document.getElementById('save').addEventListener('click', () => {
const box = document.createElement('div');
box.className = 'alert ' + kinds[kind];
box.textContent = '記録を保存しました';
document.getElementById('message').append(box);
});
I used this main.js and index.html as content and trimmed without --safelist. The output file was 11,779 bytes (3,313 bytes after gzip), and both the .alert-success{ and .alert-danger{ rules remained. The first line of the following output shows the file size (raw is the original size, gzip is the size after gzip) and the number of rules kept. The second line is the result from check.cjs. The box that appeared after the button press had the same style as before the fix. I ran Lighthouse once, and no warning appeared. The CSS transfer size was 3,530 bytes.
fullname raw= 11779 gzip= 3313 .alert{=1 .alert-success{=1 .alert-danger{=1
fullname: class="alert alert-success" background=rgb(209, 231, 221) color=rgb(10, 54, 34) padding=16px border=1px solid radius=6px
The safelist adds class names to the trimming settings. The lookup table keeps the class names written directly in the JavaScript code. The two methods differ in where you add an entry when you have more class types.
After trimming with PurgeCSS, check not only that the Lighthouse warning has gone, but also that the appearance is not broken. Perform the actions that add classes through JavaScript, such as pressing a button or opening a menu.
Unused CSS under 10 KiB was not listed
The warning also went away without removing all the unused CSS with PurgeCSS. I created a file that keeps the spacing classes (those starting with m or p, such as mt-3 and px-2) and the grid classes (those starting with col-). I called PurgeCSS from JavaScript and passed a regular expression in the safelist.
import { PurgeCSS } from 'purgecss';
import { writeFileSync } from 'node:fs';
const [r] = await new PurgeCSS().purge({
content: ['src/index.html', 'src/main.js'],
css: ['node_modules/bootstrap/dist/css/bootstrap.min.css'],
safelist: { standard: ['alert-success', /^(col-|[mp][tbsexy]?-)/] },
});
writeFileSync(process.argv[2], r.css);
The output file was 39,649 bytes (6,249 bytes after gzip) and still contains many classes this page does not use. Even so, Lighthouse did not raise the warning in any of the three runs. The following lines are the warning line and the CSS transfer size line from the first summarize.mjs output.
unused-css-rules: score=1 scoreDisplayMode=metricSavings title="Reduce unused CSS" displayValue="" metricSavings={"FCP":0,"LCP":0}
stylesheets loaded: /extra/style.css(6474B)
Lighthouse's official documentation says that stylesheets with at least 2 KiB of potential savings are listed. However, in the source code of Lighthouse 13.6.0 (unused-css-rules.js), the code lists only files whose unused amount exceeds 10 * 1024 bytes. The comment says this allows for styles that are not used just by opening the page, such as :hover. A file that transfers 6,474 bytes cannot have more than 10 KiB unused, so it is not listed.
Removing this warning and making the CSS as small as possible are not the same thing. On this page, the file that kept the spacing and grid classes (6,474 bytes transferred) also cleared the warning, but the file where I added just one class to the safelist (3,501 bytes) transferred less.
Render-blocking requests remained
Even after I made the CSS smaller with PurgeCSS, the separate audit "Render-blocking requests" still appeared on every page. The estimated FCP savings (metricSavings) dropped from 300 ms before the fix to 50–150 ms. The following is the line for this audit from the third summarize.mjs output for the page with the safelist added.
render-blocking-insight: score=0 displayValue="Est savings of 150 ms" metricSavings={"FCP":150,"LCP":150}
web.dev's explanation of deferring non-critical CSS says that a CSS file is a resource that blocks rendering, and the browser does not paint the page until it has loaded and processed the file. Making the file smaller does not change the fact that it is loaded with <link rel="stylesheet">. The same article describes how to write the CSS needed for the first display directly in a <style> element in the <head> and load the rest later. This item needs a different fix from the ones above.





