30

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

pbonnefoi
  • 263
  • 5
  • 16
corroded
  • 21,406
  • 19
  • 83
  • 132
  • Did the number of selectors turn out to be the problem? If not, what was? I'm running into the same issue. All of a sudden IE9 and only IE9 is displaying my sites incorrectly... – 9ete Jul 29 '16 at 19:35

8 Answers8

68

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);
}
Eric Bréchemier
  • 1,908
  • 3
  • 18
  • 30
jetli13
  • 593
  • 5
  • 9
  • 1
    Thanks for posting an answer! While a code snippet could answer the question it's still great to add some addition information around, like explain, etc .. – j0k Sep 26 '12 at 07:59
  • 1
    Ok. My english is very poor, but I will try ) – jetli13 Nov 22 '12 at 12:34
  • 4
    @jetli13 My Firebug says `Error: The operation is insecure. "rules = styleSheet.cssRules,"`. Any ideas? FB 1.11.2 on FF 20.0.1 – RaphaelDDL Apr 30 '13 at 21:08
  • @RaphaelIDDL I think answer you need is here [link](http://stackoverflow.com/questions/15229330/attempt-to-add-a-rule-to-a-css-stylesheet-gives-the-operation-is-insecure-in-f) – jetli13 Aug 02 '13 at 09:48
  • 4
    Worked for me in Chrome console too. – Nils Nov 17 '13 at 04:53
4

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.

Paul Young
  • 1,489
  • 1
  • 15
  • 34
3

There is this bookmarklet that tells you the number of used CSS rules out of the total CSS rules (which you are interested in).

CSS Crunch

Adam Ayres
  • 8,732
  • 1
  • 32
  • 25
1

Search & replace "{" by "{" in your CSS file. Most editors wil tell how many replacements you’ve done…

Julien B
  • 71
  • 5
  • 2
    This will only count the number of rules, not the number of selectors. There can be multiple selectors per rule, e.g. `h1, h2, h3 { margin: 0; }` – Philip Walton Jul 18 '12 at 23:24
  • I think you should count the number of { plus the number of , (comma). As far as I can see, this would be a very simple detection mechanism. – Wouter Jul 14 '15 at 21:13
1

Simple algorithm for counting the selectors if you want to do it as part of the build process or simply don't want to do it in JS:

Replace the texts between "{" and "}" with a "," and then calculate the number of ",". This will give you the number of selectors on the file.

1

a bit to late, but for anyone how's looking for a css selector counter: http://snippet.bevey.com/css/selectorCount.php it's very simple, and can work with more than one stylesheet, it even tells you when you hit the 4096 limit

Babybird
  • 11
  • 3
1

This will do inline CSS...

var selectors = 0;

$('style').each(function() {

   var styles = $(this).html();

   // Strip comments
   styles = styles.replace(/\/\*.+?\*\//sg, ''); 

   var matches = styles.match(/\{[\s.]*\}/g);

   selectors += matches.length;

});

jsFiddle.

alex
  • 479,566
  • 201
  • 878
  • 984
0

Building on jetli13's answer 10 years later with no IE selector limits to be seen but styledcomponents and mega CSS in JS generator's in the future, I added the following

  1. Warning over 1000 styles
  2. Script works even if you have 3rd party external rules
  3. Shows addition of styles per style tag or external stylesheets
  4. Lets you expand individual CSS Ruleset's

Script - Copy and run in console

You can run it after a route change in a SPA application to see how many rules got added.

var
  styleSheets = document.styleSheets,
  totalStyleSheets = styleSheets.length,
  overAllDocumentRules =0;
try {
for (var j = 0; j < totalStyleSheets; j++){

    try{
  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;
    }
  }
}
catch(err){
    console.warn("skipping>", styleSheet.href, err);
    console.warn(styleSheet)
}
if(totalRulesInStylesheet>1000) console.error("This stylesheet has over 1000 rules")
  console.log("Stylesheet: "+styleSheet.href);
  console.log(styleSheet)
  console.info("Total rules: "+totalRulesInStylesheet);
  overAllDocumentRules += totalRulesInStylesheet;
  console.warn("Total Document Rules > "+ overAllDocumentRules)
}
}
catch(err){
    console.warn("Error",err)

}

Cross Domain Stylesheet Rules a.k.a What is your 3rd party doing

If you need 3rd party styles in Safari web inspector do this: enter image description here

Output

enter image description here

Ramakay
  • 2,919
  • 1
  • 5
  • 21