Skip to content

Instantly share code, notes, and snippets.

@pointofpresence
Forked from michaelnordmeyer/find-unused-css.js
Last active December 21, 2023 20:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pointofpresence/bc503520c69ec5fa2dc9b76f460fd58f to your computer and use it in GitHub Desktop.
Save pointofpresence/bc503520c69ec5fa2dc9b76f460fd58f 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++) {
let rules;
try {
rules = document.styleSheets[ssi].cssRules || [];
} catch (e) {
rules = []
}
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