Unused CSS Finder and Purger
Operator: Abdulla Al Fahad. How tools are built and checked
Stylesheets only grow. A framework brings 200 KB of rules for a site that uses twelve of its components, and a redesign leaves the old selectors behind because nobody dares delete them. Finding the dead rules by eye does not scale; asking the browser whether each selector matches anything does.
This page parses your CSS into rules and at-rules, turns each selector into something the browser can test, and runs it against the HTML you supply using a parsed copy of each page. Rules with no match are dropped, and you download the smaller stylesheet along with a line-numbered list of what was removed. It checks only the pages you give it, which is the main thing to keep in mind.
Key features
- Real selector matching with querySelector across every uploaded page, so combinators, :nth-child and :not() are evaluated by the browser rather than guessed
- State pseudo-classes and pseudo-elements stripped before matching, so a:hover and input:checked + label are judged by the elements they target
- Safelist by name, glob or regular expression, and an optional script scan that keeps selectors whose class names appear in your JavaScript
- Unused @keyframes and @font-face removed when no kept rule refers to them
- Per-file byte savings, a removed-rules table with line numbers and a CSV report
How selectors are matched and what is protected
A selector is cleaned in three steps. Pseudo-elements (::before, ::selection, vendor ones such as ::-webkit-scrollbar) are removed. Pseudo-classes that depend on state (hover, focus, active, visited, checked, disabled, target, valid, placeholder-shown and similar) are removed too, because a static page never shows them although the user will. If that leaves a compound empty it becomes *, so .nav > :hover tests as .nav > *. Structural pseudo-classes such as :first-child, :nth-child() and :root stay and are evaluated for real. A :not() or :has() that contained a state pseudo is dropped entirely, because its result cannot be decided without the state.
A rule is kept when at least one of its selectors is used, safelisted, found in scripts or cannot be analysed. Unused selectors are removed from the list, so .btn, .btn-old { ... } becomes .btn { ... }. Selectors that the browser rejects, and :host rules (which target a shadow root, not the document), are kept and counted as not analysable. An @media or @supports block that ends up empty is removed along with its prelude.
Safelist lines work against every class name, id and element name in a selector as well as the whole selector text. A line of is-open protects any selector that contains the class is-open or the id is-open; is-* protects every class that starts with is-; /^js-/ is tested as a regular expression. With Keep selectors whose class names appear in scripts on, the page collects word-like tokens from script tags and event-style attributes (onclick, x-on, @click, data-bs-*) and keeps a selector when all of its classes and ids are among them. That catches classList.add('open') but not a class built by string concatenation.
@keyframes and @font-face are checked after the rules are pruned. An animation name counts as used when it appears in an animation or animation-name declaration of a kept rule or an inline style; a font counts as used when its family name appears in a font or font-family declaration. If any of those declarations uses var(), the names cannot be traced, so all keyframes and font faces are kept and a note says so. Rules written with CSS nesting are kept or dropped as a whole according to their outer selector.
Common use cases
- Trimming a Bootstrap or Tailwind build copied into a static site down to what its pages actually use
- Checking a stylesheet after a redesign for selectors that no longer match any template
- Producing a before-and-after size figure for a performance ticket
- Finding which @keyframes survived a component removal
How to use this tool
- Add every HTML page or template that uses the stylesheet, by file or by pasting; add the stylesheet or stylesheets.
- List runtime classes in the safelist, one per line, and decide whether to scan scripts.
- Press Find unused CSS and read the removed-rules table, which gives each selector with its line number.
- Restore anything that a page needs by adding it to the safelist and running again, then download the purged file.
Example
A page with .nav, .btn and an inline script that adds modal-open; CSS with 8 rules
.nav-link, .nav-link-old { color: #06c }
.btn.btn-lg { padding: 1rem }
.modal-open { overflow: hidden }
.js-tooltip { position: relative }
@media (max-width: 600px) { .sidebar { display: none } }
@keyframes shake { 0% { left: 0 } }Result with safelist /^js-/ and script scan on
.nav-link { color: #06c } (.nav-link-old removed from the list)
.modal-open { overflow: hidden } (kept: the class name appears in a script)
.js-tooltip { position: relative } (kept: safelist)
Removed: .btn.btn-lg (the page has .btn but never .btn-lg), the whole @media block, and @keyframes shake.Tips for best results
- Include the pages that render rarely, such as 404, login and print views; a selector used on only one of them is still used.
- Start with the safelist empty on a copy of the site, read the removed list, and add a safelist line for each class that you know scripts toggle.
- Run the purge on the exact stylesheet you ship, not a pre-processed one, so line numbers in the report match the file you edit.
Common pitfalls
Content that exists only after JavaScript runs is invisible here, because the pages are parsed, not executed. Single-page apps should be checked with their rendered HTML saved from the browser, or protected with the safelist.
Classes assembled in code, like 'btn-' + size, will not be found by the script scan and need safelist entries such as btn-*.
A stylesheet shared by several sites or pages you did not add will lose rules they depend on. Treat the output as a candidate and compare it before replacing the original.
Rules inside @media, @supports and @container blocks are judged only by whether an element matches. The query itself is not evaluated, so a print stylesheet rule is kept if its selector matches anything on the screen version of the page.
Attribute selectors on dynamic values, for example [data-state=open], match only if some page contains that attribute value in its markup.
Common errors and how to fix them
- Almost everything is reported as unused
- The HTML had few elements, often because an app shell (a single div) was saved instead of the rendered page. Save the page from the browser after it loads, or paste its full source.
- "Ignored safelist patterns that are not valid regular expressions"
- A line wrapped in slashes did not compile, for example /[a-/ . Fix the pattern or write the name or a glob instead.
Best practices
- Review the removed list before replacing a production stylesheet; the table shows each selector with its line so you can restore a rule that a page needs.
- Re-run the check whenever a template is added, since a new page can start using a class that was reported unused.
Frequently asked questions
Does it fetch the stylesheets linked from my pages?
No. It lists the stylesheet URLs it finds in the HTML to remind you which files to add, but it never downloads anything; you upload or paste the CSS yourself.