I wrote a short Ruby script that, with the help of the wonderful Hpricot HTML parser and tree library, lets you check for ‘dead’ CSS rules or selectors (that are unused in that trying to match them to any of the documents you pass it fails).
- You must have Hpricot installed.
- You pass it a number of CSS files, the
--(two dash) separator and then a number of HTML files. (CSS file means either a clean CSS file you use somewhere or an HTML file, which will be juiced for the CSS content in all its<style>elements, but not its@imports or its<link>ed stylesheets since that’s too complicated for now.) - If you have one HTML file containing CSS in-line with the
<style>element, you can just pass that one file. csscleannever touches your files; it just lists the ‘dead’ selectors it finds as well as the ‘dead’ rules it finds (where all of the rule’s selectors are ‘dead’).cssclean, for matching purposes, treats:hover,::before,:targetand a bunch of other pseudo-classes that Hpricot can’t by itself handle, as the element itself to avoid erroneously listing any:hoverselector as unused.- This was a rush job so it could do a lot more and a lot better. However, it does what it does well.
- License: none. Copyright: none. Public domain.