How do you find unused CSS?
Enter a URL and the finder loads that page in Chrome twice: on a phone screen 360 pixels wide and on a desktop screen 1350 pixels wide. Each time it scrolls to the bottom, opens the menus and toggles, hovers over the navigation and a few buttons, and focuses the first form field. Then it lists per stylesheet which rules the page used and which matched nothing, and gives you each stylesheet as an annotated copy and as a cleaned file without the unused rules.
The table shows per stylesheet:
- Render-blocking: the file is in the head, and the browser paints nothing until it has arrived.
- Compressed: the size over the network. The finder checks a render-blocking file against about 15 kB.
- Used: the share of the file's bytes the page used at either screen size or on hover, focus or tap.
- Unused: the kB the page didn't use, uncompressed, with the rules for print and other screen sizes.
Open a file for its unused rules by selector, line and size. The annotated copy is your stylesheet with a comment before every unused rule; start from the cleaned file only when you check it on every template.
Start with the render-blocking files. On a large web magazine the finder found 4 stylesheets, 21.9% of the CSS used, and 13.6 kB of render-blocking CSS compressed. That is under 15 kB, so trimming those files gains the visitor little. One blocking file of 60 kB is where removing CSS pays off.
Unused CSS in short
Unused CSS is CSS a page downloads that styles nothing on it. In a render-blocking stylesheet over about 15 kB compressed it delays the first paint: the browser waits for the whole file, which needs more than the first round trip. According to the State of Web Vitals, the median home page leaves 88% of its CSS unused.
How do you remove unused CSS?
Remove unused CSS where it comes from: load each plugin's or section's stylesheet only on the pages that use it, split a big theme file so each kind of page gets the parts it needs in files of about 15 kB compressed, and let your build drop what no template uses.
- Plugin and app CSS. Switch a form or slider plugin's stylesheet off on the pages that don't have one (how on WordPress).
- A theme built on a framework. With Bootstrap, import only the Sass parts the theme uses. Tailwind generates only the classes it finds in your templates. During the build, PurgeCSS removes every selector it can't find in your templates, so put the classes a script adds, such as
is-open, in its safelist. - Your own CSS. A rule only one template uses moves to that template's file. A rule no template uses, print and dark mode aside, can go at the source (how to check that per template).
A shared stylesheet of about 15 kB can be partly unused on every page: the browser caches it after the first one. The page on Lighthouse's Reduce unused CSS notice has the full method per source.
When is a cleaned stylesheet a good idea?
Starting from the finder's cleaned file can be smart when you remove unused CSS on purpose: you test every template and check the result on a preview before it goes live. Cleaned on one URL and pasted back, the file loses the styles of your other pages and of every state the finder couldn't reach, such as a filled cart.
- Run the finder on one URL per template: the home page, a category or archive, an article, a product, the cart and a page with a form.
- Start from the cleaned file of your busiest template. It already keeps the rules used on hover, focus or tap and the rules marked "not tested" (print, another width, dark mode).
- Put back what the other templates used, each rule in its place from their annotated copies: a rule added at the end of the file can win over a rule it lost to before.
- Put the new file on a preview and check it logged in and logged out, on a phone and on a desktop: the menus, every form, the cart and checkout, the print view and dark mode.
- Then let it go live, and run the finder again after every theme or plugin update.
Leave Remove Unused CSS in WP Rocket and Perfmatters, Generate UCSS in LiteSpeed Cache and the like switched off. They do the same job blind on every page and cause as much trouble as critical CSS: a menu or cart drawer the scan missed loses its styles, and the CSS they keep usually goes into the HTML, which every page view downloads again.
Tools that remove unused CSS online work from the page's HTML. UnCSS Online checks each selector against that HTML once, without a click, so a class added on a tap is lost. PurifyCSS Online and PurgeCSS keep a selector when its words appear in your files. On Bootstrap's album example page, PurifyCSS Online kept 30% of the 232 kB bootstrap.min.css and still dropped .collapsing, the class Bootstrap puts on the menu while it opens or closes, so the menu loses its styles in between. On the same page the finder kept .collapsing as used, also in its cleaned file: 8.1 kB compressed, from 31.1 kB.
How is the finder different from DevTools Coverage and Lighthouse?
CSS coverage in Chrome DevTools shows what one page used while you recorded: only the menus you opened and the window sizes you tried. The Reduce unused CSS notice in Lighthouse and PageSpeed Insights checks the page once after it loads, at one screen size with nobody tapping, and lists a stylesheet when more than 10 KiB of it, compressed, matched nothing. Lighthouse gives the notice no weight of its own in the Performance score: FCP and LCP show what the CSS does to the page.
To record coverage yourself, open DevTools, press Cmd+Shift+P (Ctrl+Shift+P on Windows), type Show Coverage and click the reload button in the Coverage panel.
The finder tests two screen sizes and opens the menus itself, keeps the rules used on hover, focus or tap out of the unused list, and checks each render-blocking file against 15 kB.
When does a CSS rule count as used?
A rule counts as used when Chrome matched it on the phone or the desktop screen, or after a click, hover or focus. The finder reads this from Chrome's rule tracking, the same data the Coverage panel shows.
- Used on hover, focus or tap: the rule matched nothing during the run, but its selector matches an element once
:hover,:focus,::beforeand the like are taken off. - Not tested at this screen size: the rule sits in a media query neither screen meets, such as print, a tablet width or dark mode. The finder lists it as unused, with its media query; the cleaned file still has it.
- Fonts and animations: an
@font-faceor@keyframesrule counts as used when a used rule names its font family or animation.
The result leaves out pages behind a login, content a script adds after the finder is done (a run ends at 45 seconds at the latest), and states the finder can't create, such as a filled cart or a logged-in header. Check those yourself before you remove their rules.
Unused CSS on WordPress and Shopify
On WordPress most unused CSS comes from plugins that load their stylesheet on every page. Unload a plugin's CSS on the pages that don't use it, with Asset CleanUp (free) or the Script Manager in Perfmatters, instead of switching on Remove Unused CSS.
On a WordPress theme demo the finder found 17 stylesheets, all render-blocking, 31 kB compressed together, with 30.7% of the CSS used. There the 17 files come first, each a request the first paint waits for: switch off the files this page doesn't use, then combine what's left. My own plugin, WP Core Web Vitals, puts the stylesheets a page still loads into a few files of 12 to 14 kB.
Shopify's current themes, such as Dawn and Horizon, load a section's CSS only where that section shows, so unused CSS there usually comes from an app: switch off the embeds you don't use in the theme editor under App embeds.
Stylesheets still holding up the first paint after you removed what the page doesn't use? In a power hour we go through the finder's results for your main templates and decide which files to unload, split or leave.