Skip to content

Instantly share code, notes, and snippets.

@oussla
Created December 7, 2020 18:22
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 oussla/4b40e8e6fdb0aa8936a0118028fe7466 to your computer and use it in GitHub Desktop.
Save oussla/4b40e8e6fdb0aa8936a0118028fe7466 to your computer and use it in GitHub Desktop.
CSS Selectors Count
/**
CSS Selectors Count
Count the total number of CSS selectors and provide grand total.
To be run on DevTools console.
From https://stackoverflow.com/a/12313690/1310132
*/
var
styleSheets = document.styleSheets,
totalStyleSheets = styleSheets.length,
grandTotalSelectors = 0;
for (var j = 0; j < totalStyleSheets; j++) {
var styleSheet = styleSheets[j];
try {
var rules = styleSheet.cssRules,
totalRulesInStylesheet = rules.length,
totalSelectorsInStylesheet = 0;
for (var i = 0; i < totalRulesInStylesheet; i++) {
if (rules[i].selectorText) {
totalSelectorsInStylesheet += rules[i].selectorText.split(',').length;
}
}
console.log("Stylesheet: " + styleSheet.href);
console.log("Total rules: " + totalRulesInStylesheet);
console.log("Total selectors: " + totalSelectorsInStylesheet);
grandTotalSelectors += totalSelectorsInStylesheet;
}
catch (e) {
console.log(e);
}
}
console.log("Grand Total selectors: " + grandTotalSelectors);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment