Count number of selectors in a css file

前端 未结 7 672
予麋鹿
予麋鹿 2020-12-12 15:12

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

相关标签:
7条回答
  • 2020-12-12 15:38

    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);
    }
    
    0 讨论(0)
  • 2020-12-12 15:41

    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.

    0 讨论(0)
  • 2020-12-12 15:41

    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

    0 讨论(0)
  • 2020-12-12 15:42

    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.

    0 讨论(0)
  • 2020-12-12 15:43

    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

    0 讨论(0)
  • 2020-12-12 15:50

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

    0 讨论(0)
提交回复
热议问题