Skip to content

Instantly share code, notes, and snippets.

@michaelnordmeyer
Created March 8, 2023 12:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save michaelnordmeyer/af539133a34d1764eca38c6391aea255 to your computer and use it in GitHub Desktop.
Save michaelnordmeyer/af539133a34d1764eca38c6391aea255 to your computer and use it in GitHub Desktop.
Find unused CSS rules on a page
(function() {
for (var ssi = 0; ssi < document.styleSheets.length; ssi++) {
var rules = document.styleSheets[ssi].cssRules || [];
var sheetHref = document.styleSheets[ssi].href || 'inline';
for (var ri = 0; ri < rules.length; ri++) {
if (!document.querySelectorAll(rules[ri].selectorText).length) {
console.log(`${sheetHref}: "${rules[ri].selectorText}" not found.`);
}
}
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment