How the scan decides what to keep
TrimCSS removes a CSS rule only when nothing on the pages it checked could use it. This page explains each step, so you know exactly what to expect.
Step one
Reading your pages
The scan starts with the address you enter. Our server fetches the page, in the same way a browser would, and passes it to your browser. TrimCSS then looks for your sitemap through robots.txt or the usual sitemap address, collects the links on the home page, and chooses up to 5 pages from as many different sections as it can find.
If you know which of your pages look most different from each other, choose them yourself. A scan that sees your home page, a blog post, a product and the contact page will give a far more reliable result than five similar pages.
Step two
Finding your stylesheets and scripts
Every stylesheet linked from those pages is downloaded, along with any stylesheet it imports. Scripts are read too, but only to collect the words in them: a menu script that adds a class called is-open contains those words, so rules for .is-open are kept even though the class is not in the HTML yet.
Step three
Testing every selector
Each rule is split into its separate selectors, and each selector is tested against the HTML of every page checked, using your browser's own selector engine. States such as hover, focus and checked are set aside while testing, so a:hover .icon is kept whenever the page has an icon inside a link.
A selector the browser cannot judge, such as one using syntax it does not support, is always kept. Removing it would be a guess.
Step four
Rebuilding the stylesheet
Rules where every selector is unused are taken out. Rules where only some selectors are unused keep the ones in use. Media queries, layers and supports blocks that end up empty are removed, and so are animations and web fonts that no remaining rule refers to.
The declarations inside each rule are copied exactly as written, so vendor prefixes and newer properties survive untouched. Licence comments that start with /*! are always kept.
Before you upload
Testing the trimmed file
Upload the trimmed stylesheet to a staging copy of your site, or rename it and point one page at it, then click through the pages, open the menu, send a test form and sign in if your site has accounts. If anything looks wrong, use the list of removed selectors to find the class, add it to the safelist and scan again.
Questions
Questions about the scan
How does TrimCSS pick which pages to check?
It always checks the address you enter first. It then reads your robots.txt and sitemap, adds the links on your home page, and picks pages from different sections of the site so that one scan sees as many different layouts as possible.
Why is a rule I use shown as unused?
The rule probably styles something that is not in the HTML of the pages checked: a page that was not part of the scan, a message shown after a form is sent, or an element a script builds from scratch. Add the page to your list, or add the class to the safelist, and scan again.
What does the scanner look like in my server logs?
Requests come from our server with a user agent that includes the word TrimCSS and a link to this page. Each scan reads only the pages, stylesheets, scripts, robots.txt and sitemap it needs.
Are CSS frameworks such as Tailwind supported?
Yes. Any stylesheet the page links to is checked rule by rule, whatever produced it. Escaped class names such as md:flex and w-1/2 are matched correctly.