Hmm.. I'd throw some brute force at this by extracting the various CSS selectors using a serverside parsing of the CSS file, then run each of these as a jQuery selector within the browser. Not very elegant, but should work?
I've used Dust-Me Selectors before, which is a Firefox plugin. It's very easy to use and versatile as it maintains a combined list across a number of pages of which CSS values are used.
The downside is that I wasn't able to automate it to spider an entire site, so I ended up using it just on key pages/templates of my site. It is very useful nonetheless.
This Firefox extension will probably solve your problem, Dust-Me Selectors. There's also a tiny desktop app called CssCleaner or CssHelper but I was unable to find a link to it... (just have it here at my machine downloaded a long time ago for a similar task)
There is an open source ad-on of chrome named SnappySnippet I found it lot better than other just extends the already available developer tools in chrome. You can even extract only one part of the page will all relevant css. Look at this stackoverflow post
Hands down the best tool that actually does exactly what you want by only getting the used CSS on the page and allows you to simply copy it to your clipboard is this Chrome extension CSS Used
If you're dealing with single pages, you can also use my bookmarklet to quickly grab only the CSS that is actually used by the web page:
Go here (if this link is broken, you can also get it from pastebin).
Drag the big button under "Download Bookmarklet" onto your bookmarks toolbar.
That's it. Now whenever you want to encapsulate a static page (i.e., to make it portable or if you intend to serve it from its own iframe), just click on the bookmark and it will display all the used CSS on the current page in an overlay. Click on the shadow to close the overlay.
One good thing with this solution is that it supports responsive pages since the media queries are also extracted. As a bonus, media queries are sorted by viewport size specificity (e.g., @media (max-width: 767px) will come after@media (max-width: 1023px)).
If there's a need, I can also add an option to minify the generated CSS. Since I've only used this bookmarklet for my own needs, it hasn't been widely tested, so please report any issues in the comments.
NOTE: To make this bookmarklet work with local files in Chrome, add --allow-file-access-from-files to the Chrome shortcut target. Example:
Check for PurifyCSS, and this for those who can handle CLI or Gulp/Grunt/Webpack
You can remove the unused style from single page or multiple page or from the entire project, even though the classes are being injected by javascript.
If you can google, there are tons of resources out there from which you can learn about PurifyCSS.
I came across Uncss-Online - Unofficial server, very convenient for testing or one-off usage! I think its the best tool I've come across.
UnCSS is a tool that removes unused CSS from your stylesheets. It works across multiple files and supports Javascript-injected CSS. It can be used in this way:
Copy & paste your HTML and CSS into provided boxes
Click button
Wait for magic to happen
Unused CSS is gone, take the rest and use it!
You can check their Github Page for other advanced ways to use this tool
Try using this tool,which is just a simple js script
https://github.com/shashwatsahai/CSSExtractor/
This tool helps in getting the CSS from a specific page listing all sources for active styles and save it to a JSON with source as key and rules as value.
It loads all the CSS from the href links and tells all the styles applied from them.
You can save the output in a JSON file with any name.
You can also use http://getcssusedinapage.com to get the CSS used in a page. It's free, fast & gives you back much details along with minimified & optimized CSS code that you can just copy + paste in your website
As of Sept 2020 this question is almost 10 years old. Most of the provided solutions no longer work or the linked projects have disappeared.
However, the question is still extremely relevant as Google now uses page speed as one of its prioritization metrics.
After doing a bunch of research into all the links listed, I found purgecss.com. This seems to be the best option to clean up unused CSS in modern web apps/SPAs using Angular, React, Vue, etc. There's also build integration with PostCSS, Webpack, Grunt, and Gulp.
Also, UnCSS still seems to be maintained. It's as powerful as PurgeCSS but not as integrated into build processes or single-page javascript apps.