Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count number of selectors in a css file

is there an existing plugin/app/program/script/whatever that analyzes and counts the css selectors of a file? i want to check if the reason my css file is not working in IE is because my selector count is over 4095 (which im pretty sure is not)

thanks!

p.s. plus points if there's a haml/sass/compass solution

like image 689
corroded Avatar asked Mar 08 '11 04:03

corroded


People also ask

How many CSS selectors do we have?

We can divide CSS selectors into five categories: Simple selectors (select elements based on name, id, class) Combinator selectors (select elements based on a specific relationship between them) Pseudo-class selectors (select elements based on a certain state)

Can I use multiple selectors in CSS?

A CSS selector can contain more than one simple selector. Between the simple selectors, we can include a combinator.


2 Answers

My project, Bless CSS, could be what you're looking for. It will analyze files and split them at the optimum point based on the selector limit.

It's also built in to CodeKit.

like image 34
Paul Young Avatar answered Oct 02 '22 17:10

Paul Young


The following snippet can be run in the Firebug console in Firefox to count the total number of CSS selectors (not just CSS rules) and check whether it reaches the limit of 4095 selectors per stylesheet:

var   styleSheets = document.styleSheets,   totalStyleSheets = styleSheets.length;  for (var j = 0; j < totalStyleSheets; j++){   var     styleSheet = styleSheets[j],     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); } 
like image 119
jetli13 Avatar answered Oct 02 '22 18:10

jetli13