Points in CSS specificity












119















Researching specificity I stumbled upon this blog - http://www.htmldog.com/guides/cssadvanced/specificity/



It states that specificity is a point-scoring system for CSS. It tells us that elements are worth 1 point, classes are worth 10 points and IDs are worth 100 points. It also goes on top say that these points are totaled and the overall amount is that selector's specificity.



For example:




body = 1 point
body .wrapper = 11 points
body .wrapper #container = 111 points




So, using these points surely the following CSS and HTML will result in the text being blue:



CSS:



#a {
color: red;
}

.a .b .c .d .e .f .g .h .i .j .k .l .m .n .o {
color: blue;
}


HTML:



<div class="a">
<div class="b">
<div class="c">
<div class="d">
<div class="e">
<div class="f">
<div class="g">
<div class="h">
<div class="i">
<div class="j">
<div class="k">
<div class="l">
<div class="m">
<div class="n">
<div class="o" id="a">
This should be blue.
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>


RESULT:



http://jsfiddle.net/hkqCF/



Why is the text red when 15 classes would equal 150 points compared to 1 ID which equals 100 points?



EDIT:



So apparently the points aren’t just totalled, they’re concatenated. Read more about that here - http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html
BUT, does that mean that the classes in our selector = 0,0,15,0 OR0,1,5,0?



My instincts tell me it’s the former as we KNOW the ID selector’s specificity looks like this: 0,1,0,0










share|improve this question

























  • I am going to go ahead and assume that the classes don't stack. Very interesting topic though..

    – Sphvn
    May 11 '10 at 8:25













  • Just added some more information I've just read about.

    – Sam
    May 11 '10 at 8:47











  • I love that article, would be nice to see an update to include attributes as selectors being explained/thrown into the mix.

    – Jayx
    Jul 11 '12 at 9:12











  • Here is something odd too : stackoverflow.com/questions/25565928/…

    – Armel Larcier
    Aug 29 '14 at 10:04


















119















Researching specificity I stumbled upon this blog - http://www.htmldog.com/guides/cssadvanced/specificity/



It states that specificity is a point-scoring system for CSS. It tells us that elements are worth 1 point, classes are worth 10 points and IDs are worth 100 points. It also goes on top say that these points are totaled and the overall amount is that selector's specificity.



For example:




body = 1 point
body .wrapper = 11 points
body .wrapper #container = 111 points




So, using these points surely the following CSS and HTML will result in the text being blue:



CSS:



#a {
color: red;
}

.a .b .c .d .e .f .g .h .i .j .k .l .m .n .o {
color: blue;
}


HTML:



<div class="a">
<div class="b">
<div class="c">
<div class="d">
<div class="e">
<div class="f">
<div class="g">
<div class="h">
<div class="i">
<div class="j">
<div class="k">
<div class="l">
<div class="m">
<div class="n">
<div class="o" id="a">
This should be blue.
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>


RESULT:



http://jsfiddle.net/hkqCF/



Why is the text red when 15 classes would equal 150 points compared to 1 ID which equals 100 points?



EDIT:



So apparently the points aren’t just totalled, they’re concatenated. Read more about that here - http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html
BUT, does that mean that the classes in our selector = 0,0,15,0 OR0,1,5,0?



My instincts tell me it’s the former as we KNOW the ID selector’s specificity looks like this: 0,1,0,0










share|improve this question

























  • I am going to go ahead and assume that the classes don't stack. Very interesting topic though..

    – Sphvn
    May 11 '10 at 8:25













  • Just added some more information I've just read about.

    – Sam
    May 11 '10 at 8:47











  • I love that article, would be nice to see an update to include attributes as selectors being explained/thrown into the mix.

    – Jayx
    Jul 11 '12 at 9:12











  • Here is something odd too : stackoverflow.com/questions/25565928/…

    – Armel Larcier
    Aug 29 '14 at 10:04
















119












119








119


62






Researching specificity I stumbled upon this blog - http://www.htmldog.com/guides/cssadvanced/specificity/



It states that specificity is a point-scoring system for CSS. It tells us that elements are worth 1 point, classes are worth 10 points and IDs are worth 100 points. It also goes on top say that these points are totaled and the overall amount is that selector's specificity.



For example:




body = 1 point
body .wrapper = 11 points
body .wrapper #container = 111 points




So, using these points surely the following CSS and HTML will result in the text being blue:



CSS:



#a {
color: red;
}

.a .b .c .d .e .f .g .h .i .j .k .l .m .n .o {
color: blue;
}


HTML:



<div class="a">
<div class="b">
<div class="c">
<div class="d">
<div class="e">
<div class="f">
<div class="g">
<div class="h">
<div class="i">
<div class="j">
<div class="k">
<div class="l">
<div class="m">
<div class="n">
<div class="o" id="a">
This should be blue.
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>


RESULT:



http://jsfiddle.net/hkqCF/



Why is the text red when 15 classes would equal 150 points compared to 1 ID which equals 100 points?



EDIT:



So apparently the points aren’t just totalled, they’re concatenated. Read more about that here - http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html
BUT, does that mean that the classes in our selector = 0,0,15,0 OR0,1,5,0?



My instincts tell me it’s the former as we KNOW the ID selector’s specificity looks like this: 0,1,0,0










share|improve this question
















Researching specificity I stumbled upon this blog - http://www.htmldog.com/guides/cssadvanced/specificity/



It states that specificity is a point-scoring system for CSS. It tells us that elements are worth 1 point, classes are worth 10 points and IDs are worth 100 points. It also goes on top say that these points are totaled and the overall amount is that selector's specificity.



For example:




body = 1 point
body .wrapper = 11 points
body .wrapper #container = 111 points




So, using these points surely the following CSS and HTML will result in the text being blue:



CSS:



#a {
color: red;
}

.a .b .c .d .e .f .g .h .i .j .k .l .m .n .o {
color: blue;
}


HTML:



<div class="a">
<div class="b">
<div class="c">
<div class="d">
<div class="e">
<div class="f">
<div class="g">
<div class="h">
<div class="i">
<div class="j">
<div class="k">
<div class="l">
<div class="m">
<div class="n">
<div class="o" id="a">
This should be blue.
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
</div>


RESULT:



http://jsfiddle.net/hkqCF/



Why is the text red when 15 classes would equal 150 points compared to 1 ID which equals 100 points?



EDIT:



So apparently the points aren’t just totalled, they’re concatenated. Read more about that here - http://www.stuffandnonsense.co.uk/archives/css_specificity_wars.html
BUT, does that mean that the classes in our selector = 0,0,15,0 OR0,1,5,0?



My instincts tell me it’s the former as we KNOW the ID selector’s specificity looks like this: 0,1,0,0







css css-selectors css-specificity






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 29 '16 at 23:17









Igor Ivancha

2,71842235




2,71842235










asked May 11 '10 at 8:17









SamSam

2,279103452




2,279103452













  • I am going to go ahead and assume that the classes don't stack. Very interesting topic though..

    – Sphvn
    May 11 '10 at 8:25













  • Just added some more information I've just read about.

    – Sam
    May 11 '10 at 8:47











  • I love that article, would be nice to see an update to include attributes as selectors being explained/thrown into the mix.

    – Jayx
    Jul 11 '12 at 9:12











  • Here is something odd too : stackoverflow.com/questions/25565928/…

    – Armel Larcier
    Aug 29 '14 at 10:04





















  • I am going to go ahead and assume that the classes don't stack. Very interesting topic though..

    – Sphvn
    May 11 '10 at 8:25













  • Just added some more information I've just read about.

    – Sam
    May 11 '10 at 8:47











  • I love that article, would be nice to see an update to include attributes as selectors being explained/thrown into the mix.

    – Jayx
    Jul 11 '12 at 9:12











  • Here is something odd too : stackoverflow.com/questions/25565928/…

    – Armel Larcier
    Aug 29 '14 at 10:04



















I am going to go ahead and assume that the classes don't stack. Very interesting topic though..

– Sphvn
May 11 '10 at 8:25







I am going to go ahead and assume that the classes don't stack. Very interesting topic though..

– Sphvn
May 11 '10 at 8:25















Just added some more information I've just read about.

– Sam
May 11 '10 at 8:47





Just added some more information I've just read about.

– Sam
May 11 '10 at 8:47













I love that article, would be nice to see an update to include attributes as selectors being explained/thrown into the mix.

– Jayx
Jul 11 '12 at 9:12





I love that article, would be nice to see an update to include attributes as selectors being explained/thrown into the mix.

– Jayx
Jul 11 '12 at 9:12













Here is something odd too : stackoverflow.com/questions/25565928/…

– Armel Larcier
Aug 29 '14 at 10:04







Here is something odd too : stackoverflow.com/questions/25565928/…

– Armel Larcier
Aug 29 '14 at 10:04














7 Answers
7






active

oldest

votes


















132














Pekka's answer is practically correct, and probably the best way to think about the issue.



However, as many have already pointed out, the W3C CSS recommendation states that "Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity." So the geek in me just had to figure out just how large this base is.



It turns out that the "very large base" employed (at least by the 4 most commonly-used browsers*) to implement this standard algorithm is 256 or 28.



What this means is that a style specified with 0 ids and 256 class-names will over-ride a style specified with just 1 id. I tested this out with some fiddles:




  • 255 classes are not enough to override 1 id

  • ...but 256 classes are enough to override 1 id

  • ...and 256 tag-names are enough to override 1 class-name


  • ...but, alas 256 ids are not enough to override 1 inline style (Updated 2012/8/15 -- you'll have to use !important)



So there is, effectively, a "point system," but it's not base 10. It's base 256. Here's how it works:



(28)2 or 65536, times the number of ids in the selector

+ (28)1 or 256, times the number of class-names in the selector

+ (28)0 or 1, times the number of tag-names in the selector



This isn't very practical for back-of-the-envelop exercises to communicate the concept.

That's probably why articles on the topic have been using base 10.



***** [Opera uses 216 (see karlcow’s comment). Some other selector engines use infinity — effectively no points system (see Simon Sapin’s comment).]



Update, July 2014:

As Blazemonger pointed out earlier in the year, webkit browsers (chrome, safari) now appear to use a higher base than 256. Perhaps 216, like Opera? IE and Firefox still use 256.






share|improve this answer





















  • 34





    Important: note that the number 256 is not in the spec. Thus, this answer describes an (admittedly useful) implementation detail.

    – Matt Fenwick
    Aug 15 '12 at 18:09








  • 6





    Not only 256 is not in the spec as @MattFenwick said, but it also varies across implementation. It is apparently larger in Opera. In WeasyPrint and cssselect it is "infinity": I use a tuple of integers instead of a single integer.

    – Simon Sapin
    Aug 16 '12 at 10:12






  • 3





    @Faust opera uses 16 bits instead of 8

    – karlcow
    Aug 16 '12 at 10:21






  • 4





    This answer has a more practical description than Pekka's answer, to be honest. Basically what @Matt Fenwick says: what you're describing is a practical implementation of the spec. A flawed one at that, but not one that anything should be done about, be it by authors or implementers.

    – BoltClock
    Aug 17 '12 at 9:35








  • 6





    256 appears to be insufficient in current versions of webkit (Chrome and Safari), further underscoring @MattFenwick's point.

    – Blazemonger
    Feb 12 '14 at 13:42





















27














Good question.



I can't tell for sure - all the articles I manage to find avoid the example of multiple classes, e.g. here - but I assume that when it comes to comparing the specifity between a class selector and an ID, the class gets calculated with a value of 15 only, no matter how detailed it is.



That matches my experience in how specificity behaves.



However, there must be some stacking of classes because



.a .b .c .d .e .f .g .h .i .j .k .l .m .n .o


is more specific than



.o


the only explanation I have is that the specificity of stacked classes is calculated only against each other but not against IDs.



Update: I half-way get it now. It is not a points system, and the information about classes weighing 15 points is incorrect. It is a 4-part numbering system very well explained here.



The starting point is 4 figures:



style  id   class element
0, 0, 0, 0


According to the W3C explanation on specificity, the specificty values for the abovementioned rules are:



#a            0,1,0,0    = 100
classes 0,0,15,0 = ... see the comments


this is a numbering system with a very large (undefined?) base.



My understanding is that because the base is very large, no number in column 4 can beat a number > 0 in column 3, the same for column 2, column 1 .... Is this correct?



I'd be interested whether somebody with a better grasp at Math than me could explain th numbering system and how to convert it to decimal when the individual elements are larger than 9.






share|improve this answer


























  • Thats because its between .o & .a .b etc So it would consider them collectively. But between an ID & a class e.g: .a and #a the ID will always overpower. It will (I assume) only count between classes when there is not a more overpowering attr. E.g class will go over element and id over class.

    – Sphvn
    May 11 '10 at 8:32











  • @Ozaki that is what I am assuming too, but it contradicts what the OP is saying about the points system. There must be more in play. I'd like to see the rules behind it.

    – Pekka 웃
    May 11 '10 at 8:35











  • Just realized we've both come to the same conclusion. Excellent work!

    – Sam
    May 11 '10 at 8:48






  • 5





    For the maths side, we can work in base 16 here (because none of the individual numbers exceeds 15). So 0,1,0,0 = 0100h = 256 0,0,15,0 = 00f0h = 240 256 > 240 so the id selector wins.

    – Matthew Wilson
    May 11 '10 at 9:08








  • 1





    Yes, you can think of specificity calculations as being done in a number system with a large base. I think the term "concatenating" (also used in the spec) is a much better description though. (Came here from answering a new question which turns out to be a dupe of this one, go figure...)

    – BoltClock
    Mar 2 '12 at 21:46





















8














I am currently using the book CSS Mastery: Advanced Web Standards Solutions.



Chapter 1, page 16 says:




To calculate how specific a rule is, each type of selector is assigned
a numeric value. The specificity of a rule is then calculated by
adding up the value of each of its selectors. Unfortunately,
specificity is not calculated in base 10 but a high, unspecified, base
number. This is to ensure that a highly specific selector, such as an
ID selector, is never overridden by lots of less specific selectors,
such as type selectors.




(emphasis mine) and




The specificity of a selector is broken down into four constituent
levels: a, b, c, and d.




  • if the style is an inline style, then a = 1

  • b = the total number of id selectors

  • c = the number of class, pseudo-class, and attribute selectors

  • d = the number of type selectors and pseudo-element selectors




It goes on to say that you can often do the calculation in base-10, but only if all columns have values less than 10.





In your examples, ids are not worth 100 points; each is worth [0, 1, 0, 0] points. Therefore, one id beats 15 classes because [0, 1, 0, 0] is greater than [0, 0, 15, 0] in a high-base number system.






share|improve this answer































    8














    The current Selectors Level 4 Working Draft does a good job of describing Specificity in CSS:




    Specificities are compared by comparing the three components in order: the specificity with a larger A value is more specific; if the two A values are tied, then the specificity with a larger B value is more specific; if the two B values are also tied, then the specificity with a larger c value is more specific; if all the values are tied, the two specifities are equal.




    This means that the values A, B and C are completely independent of each other.



    15 classes doesn't give your selector a specificity score of 150, it gives it a B value of 15. A single A value is enough to overpower this.



    As a metaphor, imagine a family of 1 grand parent, 1 parent and 1 child. This could be represented as 1,1,1. If the parent has 15 children, that doesn't suddenly make them another parent (1,2,0). It means that the parent has an awful lot more responsibility than they had with just 1 child (1,1,15). ;)



    The same documentation also goes on to say:




    Due to storage limitations, implementations may have limitations on the size of A, B, or c. If so, values higher than the limit must be clamped to that limit, and not overflow.




    This has been added to tackle the problem presented in Faust's answer, whereby CSS implementations back in 2012 allowed specificity values to overflow into each other.



    Back in 2012, most browsers implemented a limitation of 255, but this limitation was allowed to overflow. 255 classes had an A,B,c specificity score of 0,255,0, but 256 classes overflowed and had an A,B,c score of 1,0,0. Suddenly our B value became our A value. The Selectors Level 4 documentation completely irradiates that problem by stating that the limit can never be allowed to overflow. With this implementation, both 255 and 256 classes would have the same A,B,c score of 0,255,0.



    The problem given in Faust's answer has since been fixed in most modern browsers.






    share|improve this answer





















    • 2





      6 years after I asked this question and it's still getting awesome replies like this - thanks for adding to the knowledge base, James. Great answer!

      – Sam
      Mar 19 '16 at 22:58






    • 1





      Thanks for this. I never got around to posting an answer. Had I done so it'd probably have just been a really long-winded version of this. Now this just needs to rise to the top...

      – BoltClock
      Apr 18 '16 at 15:03



















    4














    I am fond of comparison of Specificity ranking to Olympic Games medal table (gold first method — based first on the number of gold medals, then silver and then bronze).



    It works also with your question (huge number of selectors in one specificity group). Specificity considered each group separately. In real world I've very rarely seen case with more than a dozen selectors).



    There is also quite good specificity calculator available here. You can put your example (#a and .a .b .c .d .e .f .g .h .i .j .k .l .m .n .o) there and see the results.



    Example of Rio 2016 Olympic Games medal table looks like
    enter image description here






    share|improve this answer































      3














      I don't believe that the blog's explanation is correct. The specification is here:



      http://www.w3.org/TR/CSS2/cascade.html#specificity



      "Points" from a class selector can't add up to be more important than an "id" selector. It just doesn't work like that.






      share|improve this answer
























      • Like I said in my answer. ^^ However it does make a difference if you have more of the same type (element, class, id). But that is pretty obvious if 5things say red and 3 say blue well Ima go red.

        – Sphvn
        May 11 '10 at 8:47











      • The wording around specificity probably hasn’t changed much between CSS2 and CSS2.1, but you really should be pointing to the CSS2.1 spec in future discussions as it completely supercedes CSS2 which was in general broken at the point of release.

        – Robin Whittleton
        Aug 16 '12 at 8:49



















      1














      I would say that:



      Element < Class < ID


      I think they only stack into depending what you get if it is multiple of the same.
      So a Class will always overide the element and ID always over the Class but if it is down to which of 4 elements where 3 is to blue and 1 is to red it will be blue.



      For Example:



      .a .b .c .d .e .f .g .h .i .j .k .l
      {
      color: red;
      }

      .m .n .o
      {
      color blue;
      }


      Should turn out red.



      See Example http://jsfiddle.net/RWFWq/



      "if 5things say red and 3 say blue well Ima go red"






      share|improve this answer

























        Your Answer






        StackExchange.ifUsing("editor", function () {
        StackExchange.using("externalEditor", function () {
        StackExchange.using("snippets", function () {
        StackExchange.snippets.init();
        });
        });
        }, "code-snippets");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "1"
        };
        initTagRenderer("".split(" "), "".split(" "), channelOptions);

        StackExchange.using("externalEditor", function() {
        // Have to fire editor after snippets, if snippets enabled
        if (StackExchange.settings.snippets.snippetsEnabled) {
        StackExchange.using("snippets", function() {
        createEditor();
        });
        }
        else {
        createEditor();
        }
        });

        function createEditor() {
        StackExchange.prepareEditor({
        heartbeatType: 'answer',
        autoActivateHeartbeat: false,
        convertImagesToLinks: true,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: 10,
        bindNavPrevention: true,
        postfix: "",
        imageUploader: {
        brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
        contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
        allowUrls: true
        },
        onDemand: true,
        discardSelector: ".discard-answer"
        ,immediatelyShowMarkdownHelp:true
        });


        }
        });














        draft saved

        draft discarded


















        StackExchange.ready(
        function () {
        StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2809024%2fpoints-in-css-specificity%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        7 Answers
        7






        active

        oldest

        votes








        7 Answers
        7






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        132














        Pekka's answer is practically correct, and probably the best way to think about the issue.



        However, as many have already pointed out, the W3C CSS recommendation states that "Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity." So the geek in me just had to figure out just how large this base is.



        It turns out that the "very large base" employed (at least by the 4 most commonly-used browsers*) to implement this standard algorithm is 256 or 28.



        What this means is that a style specified with 0 ids and 256 class-names will over-ride a style specified with just 1 id. I tested this out with some fiddles:




        • 255 classes are not enough to override 1 id

        • ...but 256 classes are enough to override 1 id

        • ...and 256 tag-names are enough to override 1 class-name


        • ...but, alas 256 ids are not enough to override 1 inline style (Updated 2012/8/15 -- you'll have to use !important)



        So there is, effectively, a "point system," but it's not base 10. It's base 256. Here's how it works:



        (28)2 or 65536, times the number of ids in the selector

        + (28)1 or 256, times the number of class-names in the selector

        + (28)0 or 1, times the number of tag-names in the selector



        This isn't very practical for back-of-the-envelop exercises to communicate the concept.

        That's probably why articles on the topic have been using base 10.



        ***** [Opera uses 216 (see karlcow’s comment). Some other selector engines use infinity — effectively no points system (see Simon Sapin’s comment).]



        Update, July 2014:

        As Blazemonger pointed out earlier in the year, webkit browsers (chrome, safari) now appear to use a higher base than 256. Perhaps 216, like Opera? IE and Firefox still use 256.






        share|improve this answer





















        • 34





          Important: note that the number 256 is not in the spec. Thus, this answer describes an (admittedly useful) implementation detail.

          – Matt Fenwick
          Aug 15 '12 at 18:09








        • 6





          Not only 256 is not in the spec as @MattFenwick said, but it also varies across implementation. It is apparently larger in Opera. In WeasyPrint and cssselect it is "infinity": I use a tuple of integers instead of a single integer.

          – Simon Sapin
          Aug 16 '12 at 10:12






        • 3





          @Faust opera uses 16 bits instead of 8

          – karlcow
          Aug 16 '12 at 10:21






        • 4





          This answer has a more practical description than Pekka's answer, to be honest. Basically what @Matt Fenwick says: what you're describing is a practical implementation of the spec. A flawed one at that, but not one that anything should be done about, be it by authors or implementers.

          – BoltClock
          Aug 17 '12 at 9:35








        • 6





          256 appears to be insufficient in current versions of webkit (Chrome and Safari), further underscoring @MattFenwick's point.

          – Blazemonger
          Feb 12 '14 at 13:42


















        132














        Pekka's answer is practically correct, and probably the best way to think about the issue.



        However, as many have already pointed out, the W3C CSS recommendation states that "Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity." So the geek in me just had to figure out just how large this base is.



        It turns out that the "very large base" employed (at least by the 4 most commonly-used browsers*) to implement this standard algorithm is 256 or 28.



        What this means is that a style specified with 0 ids and 256 class-names will over-ride a style specified with just 1 id. I tested this out with some fiddles:




        • 255 classes are not enough to override 1 id

        • ...but 256 classes are enough to override 1 id

        • ...and 256 tag-names are enough to override 1 class-name


        • ...but, alas 256 ids are not enough to override 1 inline style (Updated 2012/8/15 -- you'll have to use !important)



        So there is, effectively, a "point system," but it's not base 10. It's base 256. Here's how it works:



        (28)2 or 65536, times the number of ids in the selector

        + (28)1 or 256, times the number of class-names in the selector

        + (28)0 or 1, times the number of tag-names in the selector



        This isn't very practical for back-of-the-envelop exercises to communicate the concept.

        That's probably why articles on the topic have been using base 10.



        ***** [Opera uses 216 (see karlcow’s comment). Some other selector engines use infinity — effectively no points system (see Simon Sapin’s comment).]



        Update, July 2014:

        As Blazemonger pointed out earlier in the year, webkit browsers (chrome, safari) now appear to use a higher base than 256. Perhaps 216, like Opera? IE and Firefox still use 256.






        share|improve this answer





















        • 34





          Important: note that the number 256 is not in the spec. Thus, this answer describes an (admittedly useful) implementation detail.

          – Matt Fenwick
          Aug 15 '12 at 18:09








        • 6





          Not only 256 is not in the spec as @MattFenwick said, but it also varies across implementation. It is apparently larger in Opera. In WeasyPrint and cssselect it is "infinity": I use a tuple of integers instead of a single integer.

          – Simon Sapin
          Aug 16 '12 at 10:12






        • 3





          @Faust opera uses 16 bits instead of 8

          – karlcow
          Aug 16 '12 at 10:21






        • 4





          This answer has a more practical description than Pekka's answer, to be honest. Basically what @Matt Fenwick says: what you're describing is a practical implementation of the spec. A flawed one at that, but not one that anything should be done about, be it by authors or implementers.

          – BoltClock
          Aug 17 '12 at 9:35








        • 6





          256 appears to be insufficient in current versions of webkit (Chrome and Safari), further underscoring @MattFenwick's point.

          – Blazemonger
          Feb 12 '14 at 13:42
















        132












        132








        132







        Pekka's answer is practically correct, and probably the best way to think about the issue.



        However, as many have already pointed out, the W3C CSS recommendation states that "Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity." So the geek in me just had to figure out just how large this base is.



        It turns out that the "very large base" employed (at least by the 4 most commonly-used browsers*) to implement this standard algorithm is 256 or 28.



        What this means is that a style specified with 0 ids and 256 class-names will over-ride a style specified with just 1 id. I tested this out with some fiddles:




        • 255 classes are not enough to override 1 id

        • ...but 256 classes are enough to override 1 id

        • ...and 256 tag-names are enough to override 1 class-name


        • ...but, alas 256 ids are not enough to override 1 inline style (Updated 2012/8/15 -- you'll have to use !important)



        So there is, effectively, a "point system," but it's not base 10. It's base 256. Here's how it works:



        (28)2 or 65536, times the number of ids in the selector

        + (28)1 or 256, times the number of class-names in the selector

        + (28)0 or 1, times the number of tag-names in the selector



        This isn't very practical for back-of-the-envelop exercises to communicate the concept.

        That's probably why articles on the topic have been using base 10.



        ***** [Opera uses 216 (see karlcow’s comment). Some other selector engines use infinity — effectively no points system (see Simon Sapin’s comment).]



        Update, July 2014:

        As Blazemonger pointed out earlier in the year, webkit browsers (chrome, safari) now appear to use a higher base than 256. Perhaps 216, like Opera? IE and Firefox still use 256.






        share|improve this answer















        Pekka's answer is practically correct, and probably the best way to think about the issue.



        However, as many have already pointed out, the W3C CSS recommendation states that "Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity." So the geek in me just had to figure out just how large this base is.



        It turns out that the "very large base" employed (at least by the 4 most commonly-used browsers*) to implement this standard algorithm is 256 or 28.



        What this means is that a style specified with 0 ids and 256 class-names will over-ride a style specified with just 1 id. I tested this out with some fiddles:




        • 255 classes are not enough to override 1 id

        • ...but 256 classes are enough to override 1 id

        • ...and 256 tag-names are enough to override 1 class-name


        • ...but, alas 256 ids are not enough to override 1 inline style (Updated 2012/8/15 -- you'll have to use !important)



        So there is, effectively, a "point system," but it's not base 10. It's base 256. Here's how it works:



        (28)2 or 65536, times the number of ids in the selector

        + (28)1 or 256, times the number of class-names in the selector

        + (28)0 or 1, times the number of tag-names in the selector



        This isn't very practical for back-of-the-envelop exercises to communicate the concept.

        That's probably why articles on the topic have been using base 10.



        ***** [Opera uses 216 (see karlcow’s comment). Some other selector engines use infinity — effectively no points system (see Simon Sapin’s comment).]



        Update, July 2014:

        As Blazemonger pointed out earlier in the year, webkit browsers (chrome, safari) now appear to use a higher base than 256. Perhaps 216, like Opera? IE and Firefox still use 256.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 23 '17 at 12:34









        Community

        11




        11










        answered Aug 13 '12 at 12:44









        FaustFaust

        10.7k73797




        10.7k73797








        • 34





          Important: note that the number 256 is not in the spec. Thus, this answer describes an (admittedly useful) implementation detail.

          – Matt Fenwick
          Aug 15 '12 at 18:09








        • 6





          Not only 256 is not in the spec as @MattFenwick said, but it also varies across implementation. It is apparently larger in Opera. In WeasyPrint and cssselect it is "infinity": I use a tuple of integers instead of a single integer.

          – Simon Sapin
          Aug 16 '12 at 10:12






        • 3





          @Faust opera uses 16 bits instead of 8

          – karlcow
          Aug 16 '12 at 10:21






        • 4





          This answer has a more practical description than Pekka's answer, to be honest. Basically what @Matt Fenwick says: what you're describing is a practical implementation of the spec. A flawed one at that, but not one that anything should be done about, be it by authors or implementers.

          – BoltClock
          Aug 17 '12 at 9:35








        • 6





          256 appears to be insufficient in current versions of webkit (Chrome and Safari), further underscoring @MattFenwick's point.

          – Blazemonger
          Feb 12 '14 at 13:42
















        • 34





          Important: note that the number 256 is not in the spec. Thus, this answer describes an (admittedly useful) implementation detail.

          – Matt Fenwick
          Aug 15 '12 at 18:09








        • 6





          Not only 256 is not in the spec as @MattFenwick said, but it also varies across implementation. It is apparently larger in Opera. In WeasyPrint and cssselect it is "infinity": I use a tuple of integers instead of a single integer.

          – Simon Sapin
          Aug 16 '12 at 10:12






        • 3





          @Faust opera uses 16 bits instead of 8

          – karlcow
          Aug 16 '12 at 10:21






        • 4





          This answer has a more practical description than Pekka's answer, to be honest. Basically what @Matt Fenwick says: what you're describing is a practical implementation of the spec. A flawed one at that, but not one that anything should be done about, be it by authors or implementers.

          – BoltClock
          Aug 17 '12 at 9:35








        • 6





          256 appears to be insufficient in current versions of webkit (Chrome and Safari), further underscoring @MattFenwick's point.

          – Blazemonger
          Feb 12 '14 at 13:42










        34




        34





        Important: note that the number 256 is not in the spec. Thus, this answer describes an (admittedly useful) implementation detail.

        – Matt Fenwick
        Aug 15 '12 at 18:09







        Important: note that the number 256 is not in the spec. Thus, this answer describes an (admittedly useful) implementation detail.

        – Matt Fenwick
        Aug 15 '12 at 18:09






        6




        6





        Not only 256 is not in the spec as @MattFenwick said, but it also varies across implementation. It is apparently larger in Opera. In WeasyPrint and cssselect it is "infinity": I use a tuple of integers instead of a single integer.

        – Simon Sapin
        Aug 16 '12 at 10:12





        Not only 256 is not in the spec as @MattFenwick said, but it also varies across implementation. It is apparently larger in Opera. In WeasyPrint and cssselect it is "infinity": I use a tuple of integers instead of a single integer.

        – Simon Sapin
        Aug 16 '12 at 10:12




        3




        3





        @Faust opera uses 16 bits instead of 8

        – karlcow
        Aug 16 '12 at 10:21





        @Faust opera uses 16 bits instead of 8

        – karlcow
        Aug 16 '12 at 10:21




        4




        4





        This answer has a more practical description than Pekka's answer, to be honest. Basically what @Matt Fenwick says: what you're describing is a practical implementation of the spec. A flawed one at that, but not one that anything should be done about, be it by authors or implementers.

        – BoltClock
        Aug 17 '12 at 9:35







        This answer has a more practical description than Pekka's answer, to be honest. Basically what @Matt Fenwick says: what you're describing is a practical implementation of the spec. A flawed one at that, but not one that anything should be done about, be it by authors or implementers.

        – BoltClock
        Aug 17 '12 at 9:35






        6




        6





        256 appears to be insufficient in current versions of webkit (Chrome and Safari), further underscoring @MattFenwick's point.

        – Blazemonger
        Feb 12 '14 at 13:42







        256 appears to be insufficient in current versions of webkit (Chrome and Safari), further underscoring @MattFenwick's point.

        – Blazemonger
        Feb 12 '14 at 13:42















        27














        Good question.



        I can't tell for sure - all the articles I manage to find avoid the example of multiple classes, e.g. here - but I assume that when it comes to comparing the specifity between a class selector and an ID, the class gets calculated with a value of 15 only, no matter how detailed it is.



        That matches my experience in how specificity behaves.



        However, there must be some stacking of classes because



        .a .b .c .d .e .f .g .h .i .j .k .l .m .n .o


        is more specific than



        .o


        the only explanation I have is that the specificity of stacked classes is calculated only against each other but not against IDs.



        Update: I half-way get it now. It is not a points system, and the information about classes weighing 15 points is incorrect. It is a 4-part numbering system very well explained here.



        The starting point is 4 figures:



        style  id   class element
        0, 0, 0, 0


        According to the W3C explanation on specificity, the specificty values for the abovementioned rules are:



        #a            0,1,0,0    = 100
        classes 0,0,15,0 = ... see the comments


        this is a numbering system with a very large (undefined?) base.



        My understanding is that because the base is very large, no number in column 4 can beat a number > 0 in column 3, the same for column 2, column 1 .... Is this correct?



        I'd be interested whether somebody with a better grasp at Math than me could explain th numbering system and how to convert it to decimal when the individual elements are larger than 9.






        share|improve this answer


























        • Thats because its between .o & .a .b etc So it would consider them collectively. But between an ID & a class e.g: .a and #a the ID will always overpower. It will (I assume) only count between classes when there is not a more overpowering attr. E.g class will go over element and id over class.

          – Sphvn
          May 11 '10 at 8:32











        • @Ozaki that is what I am assuming too, but it contradicts what the OP is saying about the points system. There must be more in play. I'd like to see the rules behind it.

          – Pekka 웃
          May 11 '10 at 8:35











        • Just realized we've both come to the same conclusion. Excellent work!

          – Sam
          May 11 '10 at 8:48






        • 5





          For the maths side, we can work in base 16 here (because none of the individual numbers exceeds 15). So 0,1,0,0 = 0100h = 256 0,0,15,0 = 00f0h = 240 256 > 240 so the id selector wins.

          – Matthew Wilson
          May 11 '10 at 9:08








        • 1





          Yes, you can think of specificity calculations as being done in a number system with a large base. I think the term "concatenating" (also used in the spec) is a much better description though. (Came here from answering a new question which turns out to be a dupe of this one, go figure...)

          – BoltClock
          Mar 2 '12 at 21:46


















        27














        Good question.



        I can't tell for sure - all the articles I manage to find avoid the example of multiple classes, e.g. here - but I assume that when it comes to comparing the specifity between a class selector and an ID, the class gets calculated with a value of 15 only, no matter how detailed it is.



        That matches my experience in how specificity behaves.



        However, there must be some stacking of classes because



        .a .b .c .d .e .f .g .h .i .j .k .l .m .n .o


        is more specific than



        .o


        the only explanation I have is that the specificity of stacked classes is calculated only against each other but not against IDs.



        Update: I half-way get it now. It is not a points system, and the information about classes weighing 15 points is incorrect. It is a 4-part numbering system very well explained here.



        The starting point is 4 figures:



        style  id   class element
        0, 0, 0, 0


        According to the W3C explanation on specificity, the specificty values for the abovementioned rules are:



        #a            0,1,0,0    = 100
        classes 0,0,15,0 = ... see the comments


        this is a numbering system with a very large (undefined?) base.



        My understanding is that because the base is very large, no number in column 4 can beat a number > 0 in column 3, the same for column 2, column 1 .... Is this correct?



        I'd be interested whether somebody with a better grasp at Math than me could explain th numbering system and how to convert it to decimal when the individual elements are larger than 9.






        share|improve this answer


























        • Thats because its between .o & .a .b etc So it would consider them collectively. But between an ID & a class e.g: .a and #a the ID will always overpower. It will (I assume) only count between classes when there is not a more overpowering attr. E.g class will go over element and id over class.

          – Sphvn
          May 11 '10 at 8:32











        • @Ozaki that is what I am assuming too, but it contradicts what the OP is saying about the points system. There must be more in play. I'd like to see the rules behind it.

          – Pekka 웃
          May 11 '10 at 8:35











        • Just realized we've both come to the same conclusion. Excellent work!

          – Sam
          May 11 '10 at 8:48






        • 5





          For the maths side, we can work in base 16 here (because none of the individual numbers exceeds 15). So 0,1,0,0 = 0100h = 256 0,0,15,0 = 00f0h = 240 256 > 240 so the id selector wins.

          – Matthew Wilson
          May 11 '10 at 9:08








        • 1





          Yes, you can think of specificity calculations as being done in a number system with a large base. I think the term "concatenating" (also used in the spec) is a much better description though. (Came here from answering a new question which turns out to be a dupe of this one, go figure...)

          – BoltClock
          Mar 2 '12 at 21:46
















        27












        27








        27







        Good question.



        I can't tell for sure - all the articles I manage to find avoid the example of multiple classes, e.g. here - but I assume that when it comes to comparing the specifity between a class selector and an ID, the class gets calculated with a value of 15 only, no matter how detailed it is.



        That matches my experience in how specificity behaves.



        However, there must be some stacking of classes because



        .a .b .c .d .e .f .g .h .i .j .k .l .m .n .o


        is more specific than



        .o


        the only explanation I have is that the specificity of stacked classes is calculated only against each other but not against IDs.



        Update: I half-way get it now. It is not a points system, and the information about classes weighing 15 points is incorrect. It is a 4-part numbering system very well explained here.



        The starting point is 4 figures:



        style  id   class element
        0, 0, 0, 0


        According to the W3C explanation on specificity, the specificty values for the abovementioned rules are:



        #a            0,1,0,0    = 100
        classes 0,0,15,0 = ... see the comments


        this is a numbering system with a very large (undefined?) base.



        My understanding is that because the base is very large, no number in column 4 can beat a number > 0 in column 3, the same for column 2, column 1 .... Is this correct?



        I'd be interested whether somebody with a better grasp at Math than me could explain th numbering system and how to convert it to decimal when the individual elements are larger than 9.






        share|improve this answer















        Good question.



        I can't tell for sure - all the articles I manage to find avoid the example of multiple classes, e.g. here - but I assume that when it comes to comparing the specifity between a class selector and an ID, the class gets calculated with a value of 15 only, no matter how detailed it is.



        That matches my experience in how specificity behaves.



        However, there must be some stacking of classes because



        .a .b .c .d .e .f .g .h .i .j .k .l .m .n .o


        is more specific than



        .o


        the only explanation I have is that the specificity of stacked classes is calculated only against each other but not against IDs.



        Update: I half-way get it now. It is not a points system, and the information about classes weighing 15 points is incorrect. It is a 4-part numbering system very well explained here.



        The starting point is 4 figures:



        style  id   class element
        0, 0, 0, 0


        According to the W3C explanation on specificity, the specificty values for the abovementioned rules are:



        #a            0,1,0,0    = 100
        classes 0,0,15,0 = ... see the comments


        this is a numbering system with a very large (undefined?) base.



        My understanding is that because the base is very large, no number in column 4 can beat a number > 0 in column 3, the same for column 2, column 1 .... Is this correct?



        I'd be interested whether somebody with a better grasp at Math than me could explain th numbering system and how to convert it to decimal when the individual elements are larger than 9.







        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Aug 15 '12 at 19:21









        Stephen Watkins

        16.5k85291




        16.5k85291










        answered May 11 '10 at 8:28









        Pekka 웃Pekka 웃

        357k1178411013




        357k1178411013













        • Thats because its between .o & .a .b etc So it would consider them collectively. But between an ID & a class e.g: .a and #a the ID will always overpower. It will (I assume) only count between classes when there is not a more overpowering attr. E.g class will go over element and id over class.

          – Sphvn
          May 11 '10 at 8:32











        • @Ozaki that is what I am assuming too, but it contradicts what the OP is saying about the points system. There must be more in play. I'd like to see the rules behind it.

          – Pekka 웃
          May 11 '10 at 8:35











        • Just realized we've both come to the same conclusion. Excellent work!

          – Sam
          May 11 '10 at 8:48






        • 5





          For the maths side, we can work in base 16 here (because none of the individual numbers exceeds 15). So 0,1,0,0 = 0100h = 256 0,0,15,0 = 00f0h = 240 256 > 240 so the id selector wins.

          – Matthew Wilson
          May 11 '10 at 9:08








        • 1





          Yes, you can think of specificity calculations as being done in a number system with a large base. I think the term "concatenating" (also used in the spec) is a much better description though. (Came here from answering a new question which turns out to be a dupe of this one, go figure...)

          – BoltClock
          Mar 2 '12 at 21:46





















        • Thats because its between .o & .a .b etc So it would consider them collectively. But between an ID & a class e.g: .a and #a the ID will always overpower. It will (I assume) only count between classes when there is not a more overpowering attr. E.g class will go over element and id over class.

          – Sphvn
          May 11 '10 at 8:32











        • @Ozaki that is what I am assuming too, but it contradicts what the OP is saying about the points system. There must be more in play. I'd like to see the rules behind it.

          – Pekka 웃
          May 11 '10 at 8:35











        • Just realized we've both come to the same conclusion. Excellent work!

          – Sam
          May 11 '10 at 8:48






        • 5





          For the maths side, we can work in base 16 here (because none of the individual numbers exceeds 15). So 0,1,0,0 = 0100h = 256 0,0,15,0 = 00f0h = 240 256 > 240 so the id selector wins.

          – Matthew Wilson
          May 11 '10 at 9:08








        • 1





          Yes, you can think of specificity calculations as being done in a number system with a large base. I think the term "concatenating" (also used in the spec) is a much better description though. (Came here from answering a new question which turns out to be a dupe of this one, go figure...)

          – BoltClock
          Mar 2 '12 at 21:46



















        Thats because its between .o & .a .b etc So it would consider them collectively. But between an ID & a class e.g: .a and #a the ID will always overpower. It will (I assume) only count between classes when there is not a more overpowering attr. E.g class will go over element and id over class.

        – Sphvn
        May 11 '10 at 8:32





        Thats because its between .o & .a .b etc So it would consider them collectively. But between an ID & a class e.g: .a and #a the ID will always overpower. It will (I assume) only count between classes when there is not a more overpowering attr. E.g class will go over element and id over class.

        – Sphvn
        May 11 '10 at 8:32













        @Ozaki that is what I am assuming too, but it contradicts what the OP is saying about the points system. There must be more in play. I'd like to see the rules behind it.

        – Pekka 웃
        May 11 '10 at 8:35





        @Ozaki that is what I am assuming too, but it contradicts what the OP is saying about the points system. There must be more in play. I'd like to see the rules behind it.

        – Pekka 웃
        May 11 '10 at 8:35













        Just realized we've both come to the same conclusion. Excellent work!

        – Sam
        May 11 '10 at 8:48





        Just realized we've both come to the same conclusion. Excellent work!

        – Sam
        May 11 '10 at 8:48




        5




        5





        For the maths side, we can work in base 16 here (because none of the individual numbers exceeds 15). So 0,1,0,0 = 0100h = 256 0,0,15,0 = 00f0h = 240 256 > 240 so the id selector wins.

        – Matthew Wilson
        May 11 '10 at 9:08







        For the maths side, we can work in base 16 here (because none of the individual numbers exceeds 15). So 0,1,0,0 = 0100h = 256 0,0,15,0 = 00f0h = 240 256 > 240 so the id selector wins.

        – Matthew Wilson
        May 11 '10 at 9:08






        1




        1





        Yes, you can think of specificity calculations as being done in a number system with a large base. I think the term "concatenating" (also used in the spec) is a much better description though. (Came here from answering a new question which turns out to be a dupe of this one, go figure...)

        – BoltClock
        Mar 2 '12 at 21:46







        Yes, you can think of specificity calculations as being done in a number system with a large base. I think the term "concatenating" (also used in the spec) is a much better description though. (Came here from answering a new question which turns out to be a dupe of this one, go figure...)

        – BoltClock
        Mar 2 '12 at 21:46













        8














        I am currently using the book CSS Mastery: Advanced Web Standards Solutions.



        Chapter 1, page 16 says:




        To calculate how specific a rule is, each type of selector is assigned
        a numeric value. The specificity of a rule is then calculated by
        adding up the value of each of its selectors. Unfortunately,
        specificity is not calculated in base 10 but a high, unspecified, base
        number. This is to ensure that a highly specific selector, such as an
        ID selector, is never overridden by lots of less specific selectors,
        such as type selectors.




        (emphasis mine) and




        The specificity of a selector is broken down into four constituent
        levels: a, b, c, and d.




        • if the style is an inline style, then a = 1

        • b = the total number of id selectors

        • c = the number of class, pseudo-class, and attribute selectors

        • d = the number of type selectors and pseudo-element selectors




        It goes on to say that you can often do the calculation in base-10, but only if all columns have values less than 10.





        In your examples, ids are not worth 100 points; each is worth [0, 1, 0, 0] points. Therefore, one id beats 15 classes because [0, 1, 0, 0] is greater than [0, 0, 15, 0] in a high-base number system.






        share|improve this answer




























          8














          I am currently using the book CSS Mastery: Advanced Web Standards Solutions.



          Chapter 1, page 16 says:




          To calculate how specific a rule is, each type of selector is assigned
          a numeric value. The specificity of a rule is then calculated by
          adding up the value of each of its selectors. Unfortunately,
          specificity is not calculated in base 10 but a high, unspecified, base
          number. This is to ensure that a highly specific selector, such as an
          ID selector, is never overridden by lots of less specific selectors,
          such as type selectors.




          (emphasis mine) and




          The specificity of a selector is broken down into four constituent
          levels: a, b, c, and d.




          • if the style is an inline style, then a = 1

          • b = the total number of id selectors

          • c = the number of class, pseudo-class, and attribute selectors

          • d = the number of type selectors and pseudo-element selectors




          It goes on to say that you can often do the calculation in base-10, but only if all columns have values less than 10.





          In your examples, ids are not worth 100 points; each is worth [0, 1, 0, 0] points. Therefore, one id beats 15 classes because [0, 1, 0, 0] is greater than [0, 0, 15, 0] in a high-base number system.






          share|improve this answer


























            8












            8








            8







            I am currently using the book CSS Mastery: Advanced Web Standards Solutions.



            Chapter 1, page 16 says:




            To calculate how specific a rule is, each type of selector is assigned
            a numeric value. The specificity of a rule is then calculated by
            adding up the value of each of its selectors. Unfortunately,
            specificity is not calculated in base 10 but a high, unspecified, base
            number. This is to ensure that a highly specific selector, such as an
            ID selector, is never overridden by lots of less specific selectors,
            such as type selectors.




            (emphasis mine) and




            The specificity of a selector is broken down into four constituent
            levels: a, b, c, and d.




            • if the style is an inline style, then a = 1

            • b = the total number of id selectors

            • c = the number of class, pseudo-class, and attribute selectors

            • d = the number of type selectors and pseudo-element selectors




            It goes on to say that you can often do the calculation in base-10, but only if all columns have values less than 10.





            In your examples, ids are not worth 100 points; each is worth [0, 1, 0, 0] points. Therefore, one id beats 15 classes because [0, 1, 0, 0] is greater than [0, 0, 15, 0] in a high-base number system.






            share|improve this answer













            I am currently using the book CSS Mastery: Advanced Web Standards Solutions.



            Chapter 1, page 16 says:




            To calculate how specific a rule is, each type of selector is assigned
            a numeric value. The specificity of a rule is then calculated by
            adding up the value of each of its selectors. Unfortunately,
            specificity is not calculated in base 10 but a high, unspecified, base
            number. This is to ensure that a highly specific selector, such as an
            ID selector, is never overridden by lots of less specific selectors,
            such as type selectors.




            (emphasis mine) and




            The specificity of a selector is broken down into four constituent
            levels: a, b, c, and d.




            • if the style is an inline style, then a = 1

            • b = the total number of id selectors

            • c = the number of class, pseudo-class, and attribute selectors

            • d = the number of type selectors and pseudo-element selectors




            It goes on to say that you can often do the calculation in base-10, but only if all columns have values less than 10.





            In your examples, ids are not worth 100 points; each is worth [0, 1, 0, 0] points. Therefore, one id beats 15 classes because [0, 1, 0, 0] is greater than [0, 0, 15, 0] in a high-base number system.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Feb 16 '12 at 19:01









            Matt FenwickMatt Fenwick

            32k14102169




            32k14102169























                8














                The current Selectors Level 4 Working Draft does a good job of describing Specificity in CSS:




                Specificities are compared by comparing the three components in order: the specificity with a larger A value is more specific; if the two A values are tied, then the specificity with a larger B value is more specific; if the two B values are also tied, then the specificity with a larger c value is more specific; if all the values are tied, the two specifities are equal.




                This means that the values A, B and C are completely independent of each other.



                15 classes doesn't give your selector a specificity score of 150, it gives it a B value of 15. A single A value is enough to overpower this.



                As a metaphor, imagine a family of 1 grand parent, 1 parent and 1 child. This could be represented as 1,1,1. If the parent has 15 children, that doesn't suddenly make them another parent (1,2,0). It means that the parent has an awful lot more responsibility than they had with just 1 child (1,1,15). ;)



                The same documentation also goes on to say:




                Due to storage limitations, implementations may have limitations on the size of A, B, or c. If so, values higher than the limit must be clamped to that limit, and not overflow.




                This has been added to tackle the problem presented in Faust's answer, whereby CSS implementations back in 2012 allowed specificity values to overflow into each other.



                Back in 2012, most browsers implemented a limitation of 255, but this limitation was allowed to overflow. 255 classes had an A,B,c specificity score of 0,255,0, but 256 classes overflowed and had an A,B,c score of 1,0,0. Suddenly our B value became our A value. The Selectors Level 4 documentation completely irradiates that problem by stating that the limit can never be allowed to overflow. With this implementation, both 255 and 256 classes would have the same A,B,c score of 0,255,0.



                The problem given in Faust's answer has since been fixed in most modern browsers.






                share|improve this answer





















                • 2





                  6 years after I asked this question and it's still getting awesome replies like this - thanks for adding to the knowledge base, James. Great answer!

                  – Sam
                  Mar 19 '16 at 22:58






                • 1





                  Thanks for this. I never got around to posting an answer. Had I done so it'd probably have just been a really long-winded version of this. Now this just needs to rise to the top...

                  – BoltClock
                  Apr 18 '16 at 15:03
















                8














                The current Selectors Level 4 Working Draft does a good job of describing Specificity in CSS:




                Specificities are compared by comparing the three components in order: the specificity with a larger A value is more specific; if the two A values are tied, then the specificity with a larger B value is more specific; if the two B values are also tied, then the specificity with a larger c value is more specific; if all the values are tied, the two specifities are equal.




                This means that the values A, B and C are completely independent of each other.



                15 classes doesn't give your selector a specificity score of 150, it gives it a B value of 15. A single A value is enough to overpower this.



                As a metaphor, imagine a family of 1 grand parent, 1 parent and 1 child. This could be represented as 1,1,1. If the parent has 15 children, that doesn't suddenly make them another parent (1,2,0). It means that the parent has an awful lot more responsibility than they had with just 1 child (1,1,15). ;)



                The same documentation also goes on to say:




                Due to storage limitations, implementations may have limitations on the size of A, B, or c. If so, values higher than the limit must be clamped to that limit, and not overflow.




                This has been added to tackle the problem presented in Faust's answer, whereby CSS implementations back in 2012 allowed specificity values to overflow into each other.



                Back in 2012, most browsers implemented a limitation of 255, but this limitation was allowed to overflow. 255 classes had an A,B,c specificity score of 0,255,0, but 256 classes overflowed and had an A,B,c score of 1,0,0. Suddenly our B value became our A value. The Selectors Level 4 documentation completely irradiates that problem by stating that the limit can never be allowed to overflow. With this implementation, both 255 and 256 classes would have the same A,B,c score of 0,255,0.



                The problem given in Faust's answer has since been fixed in most modern browsers.






                share|improve this answer





















                • 2





                  6 years after I asked this question and it's still getting awesome replies like this - thanks for adding to the knowledge base, James. Great answer!

                  – Sam
                  Mar 19 '16 at 22:58






                • 1





                  Thanks for this. I never got around to posting an answer. Had I done so it'd probably have just been a really long-winded version of this. Now this just needs to rise to the top...

                  – BoltClock
                  Apr 18 '16 at 15:03














                8












                8








                8







                The current Selectors Level 4 Working Draft does a good job of describing Specificity in CSS:




                Specificities are compared by comparing the three components in order: the specificity with a larger A value is more specific; if the two A values are tied, then the specificity with a larger B value is more specific; if the two B values are also tied, then the specificity with a larger c value is more specific; if all the values are tied, the two specifities are equal.




                This means that the values A, B and C are completely independent of each other.



                15 classes doesn't give your selector a specificity score of 150, it gives it a B value of 15. A single A value is enough to overpower this.



                As a metaphor, imagine a family of 1 grand parent, 1 parent and 1 child. This could be represented as 1,1,1. If the parent has 15 children, that doesn't suddenly make them another parent (1,2,0). It means that the parent has an awful lot more responsibility than they had with just 1 child (1,1,15). ;)



                The same documentation also goes on to say:




                Due to storage limitations, implementations may have limitations on the size of A, B, or c. If so, values higher than the limit must be clamped to that limit, and not overflow.




                This has been added to tackle the problem presented in Faust's answer, whereby CSS implementations back in 2012 allowed specificity values to overflow into each other.



                Back in 2012, most browsers implemented a limitation of 255, but this limitation was allowed to overflow. 255 classes had an A,B,c specificity score of 0,255,0, but 256 classes overflowed and had an A,B,c score of 1,0,0. Suddenly our B value became our A value. The Selectors Level 4 documentation completely irradiates that problem by stating that the limit can never be allowed to overflow. With this implementation, both 255 and 256 classes would have the same A,B,c score of 0,255,0.



                The problem given in Faust's answer has since been fixed in most modern browsers.






                share|improve this answer















                The current Selectors Level 4 Working Draft does a good job of describing Specificity in CSS:




                Specificities are compared by comparing the three components in order: the specificity with a larger A value is more specific; if the two A values are tied, then the specificity with a larger B value is more specific; if the two B values are also tied, then the specificity with a larger c value is more specific; if all the values are tied, the two specifities are equal.




                This means that the values A, B and C are completely independent of each other.



                15 classes doesn't give your selector a specificity score of 150, it gives it a B value of 15. A single A value is enough to overpower this.



                As a metaphor, imagine a family of 1 grand parent, 1 parent and 1 child. This could be represented as 1,1,1. If the parent has 15 children, that doesn't suddenly make them another parent (1,2,0). It means that the parent has an awful lot more responsibility than they had with just 1 child (1,1,15). ;)



                The same documentation also goes on to say:




                Due to storage limitations, implementations may have limitations on the size of A, B, or c. If so, values higher than the limit must be clamped to that limit, and not overflow.




                This has been added to tackle the problem presented in Faust's answer, whereby CSS implementations back in 2012 allowed specificity values to overflow into each other.



                Back in 2012, most browsers implemented a limitation of 255, but this limitation was allowed to overflow. 255 classes had an A,B,c specificity score of 0,255,0, but 256 classes overflowed and had an A,B,c score of 1,0,0. Suddenly our B value became our A value. The Selectors Level 4 documentation completely irradiates that problem by stating that the limit can never be allowed to overflow. With this implementation, both 255 and 256 classes would have the same A,B,c score of 0,255,0.



                The problem given in Faust's answer has since been fixed in most modern browsers.







                share|improve this answer














                share|improve this answer



                share|improve this answer








                edited May 23 '17 at 11:54









                Community

                11




                11










                answered Mar 14 '16 at 16:56









                James DonnellyJames Donnelly

                99.3k21154171




                99.3k21154171








                • 2





                  6 years after I asked this question and it's still getting awesome replies like this - thanks for adding to the knowledge base, James. Great answer!

                  – Sam
                  Mar 19 '16 at 22:58






                • 1





                  Thanks for this. I never got around to posting an answer. Had I done so it'd probably have just been a really long-winded version of this. Now this just needs to rise to the top...

                  – BoltClock
                  Apr 18 '16 at 15:03














                • 2





                  6 years after I asked this question and it's still getting awesome replies like this - thanks for adding to the knowledge base, James. Great answer!

                  – Sam
                  Mar 19 '16 at 22:58






                • 1





                  Thanks for this. I never got around to posting an answer. Had I done so it'd probably have just been a really long-winded version of this. Now this just needs to rise to the top...

                  – BoltClock
                  Apr 18 '16 at 15:03








                2




                2





                6 years after I asked this question and it's still getting awesome replies like this - thanks for adding to the knowledge base, James. Great answer!

                – Sam
                Mar 19 '16 at 22:58





                6 years after I asked this question and it's still getting awesome replies like this - thanks for adding to the knowledge base, James. Great answer!

                – Sam
                Mar 19 '16 at 22:58




                1




                1





                Thanks for this. I never got around to posting an answer. Had I done so it'd probably have just been a really long-winded version of this. Now this just needs to rise to the top...

                – BoltClock
                Apr 18 '16 at 15:03





                Thanks for this. I never got around to posting an answer. Had I done so it'd probably have just been a really long-winded version of this. Now this just needs to rise to the top...

                – BoltClock
                Apr 18 '16 at 15:03











                4














                I am fond of comparison of Specificity ranking to Olympic Games medal table (gold first method — based first on the number of gold medals, then silver and then bronze).



                It works also with your question (huge number of selectors in one specificity group). Specificity considered each group separately. In real world I've very rarely seen case with more than a dozen selectors).



                There is also quite good specificity calculator available here. You can put your example (#a and .a .b .c .d .e .f .g .h .i .j .k .l .m .n .o) there and see the results.



                Example of Rio 2016 Olympic Games medal table looks like
                enter image description here






                share|improve this answer




























                  4














                  I am fond of comparison of Specificity ranking to Olympic Games medal table (gold first method — based first on the number of gold medals, then silver and then bronze).



                  It works also with your question (huge number of selectors in one specificity group). Specificity considered each group separately. In real world I've very rarely seen case with more than a dozen selectors).



                  There is also quite good specificity calculator available here. You can put your example (#a and .a .b .c .d .e .f .g .h .i .j .k .l .m .n .o) there and see the results.



                  Example of Rio 2016 Olympic Games medal table looks like
                  enter image description here






                  share|improve this answer


























                    4












                    4








                    4







                    I am fond of comparison of Specificity ranking to Olympic Games medal table (gold first method — based first on the number of gold medals, then silver and then bronze).



                    It works also with your question (huge number of selectors in one specificity group). Specificity considered each group separately. In real world I've very rarely seen case with more than a dozen selectors).



                    There is also quite good specificity calculator available here. You can put your example (#a and .a .b .c .d .e .f .g .h .i .j .k .l .m .n .o) there and see the results.



                    Example of Rio 2016 Olympic Games medal table looks like
                    enter image description here






                    share|improve this answer













                    I am fond of comparison of Specificity ranking to Olympic Games medal table (gold first method — based first on the number of gold medals, then silver and then bronze).



                    It works also with your question (huge number of selectors in one specificity group). Specificity considered each group separately. In real world I've very rarely seen case with more than a dozen selectors).



                    There is also quite good specificity calculator available here. You can put your example (#a and .a .b .c .d .e .f .g .h .i .j .k .l .m .n .o) there and see the results.



                    Example of Rio 2016 Olympic Games medal table looks like
                    enter image description here







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Feb 2 '17 at 23:09









                    rkarczmarczykrkarczmarczyk

                    14516




                    14516























                        3














                        I don't believe that the blog's explanation is correct. The specification is here:



                        http://www.w3.org/TR/CSS2/cascade.html#specificity



                        "Points" from a class selector can't add up to be more important than an "id" selector. It just doesn't work like that.






                        share|improve this answer
























                        • Like I said in my answer. ^^ However it does make a difference if you have more of the same type (element, class, id). But that is pretty obvious if 5things say red and 3 say blue well Ima go red.

                          – Sphvn
                          May 11 '10 at 8:47











                        • The wording around specificity probably hasn’t changed much between CSS2 and CSS2.1, but you really should be pointing to the CSS2.1 spec in future discussions as it completely supercedes CSS2 which was in general broken at the point of release.

                          – Robin Whittleton
                          Aug 16 '12 at 8:49
















                        3














                        I don't believe that the blog's explanation is correct. The specification is here:



                        http://www.w3.org/TR/CSS2/cascade.html#specificity



                        "Points" from a class selector can't add up to be more important than an "id" selector. It just doesn't work like that.






                        share|improve this answer
























                        • Like I said in my answer. ^^ However it does make a difference if you have more of the same type (element, class, id). But that is pretty obvious if 5things say red and 3 say blue well Ima go red.

                          – Sphvn
                          May 11 '10 at 8:47











                        • The wording around specificity probably hasn’t changed much between CSS2 and CSS2.1, but you really should be pointing to the CSS2.1 spec in future discussions as it completely supercedes CSS2 which was in general broken at the point of release.

                          – Robin Whittleton
                          Aug 16 '12 at 8:49














                        3












                        3








                        3







                        I don't believe that the blog's explanation is correct. The specification is here:



                        http://www.w3.org/TR/CSS2/cascade.html#specificity



                        "Points" from a class selector can't add up to be more important than an "id" selector. It just doesn't work like that.






                        share|improve this answer













                        I don't believe that the blog's explanation is correct. The specification is here:



                        http://www.w3.org/TR/CSS2/cascade.html#specificity



                        "Points" from a class selector can't add up to be more important than an "id" selector. It just doesn't work like that.







                        share|improve this answer












                        share|improve this answer



                        share|improve this answer










                        answered May 11 '10 at 8:44









                        Matthew WilsonMatthew Wilson

                        3,4231512




                        3,4231512













                        • Like I said in my answer. ^^ However it does make a difference if you have more of the same type (element, class, id). But that is pretty obvious if 5things say red and 3 say blue well Ima go red.

                          – Sphvn
                          May 11 '10 at 8:47











                        • The wording around specificity probably hasn’t changed much between CSS2 and CSS2.1, but you really should be pointing to the CSS2.1 spec in future discussions as it completely supercedes CSS2 which was in general broken at the point of release.

                          – Robin Whittleton
                          Aug 16 '12 at 8:49



















                        • Like I said in my answer. ^^ However it does make a difference if you have more of the same type (element, class, id). But that is pretty obvious if 5things say red and 3 say blue well Ima go red.

                          – Sphvn
                          May 11 '10 at 8:47











                        • The wording around specificity probably hasn’t changed much between CSS2 and CSS2.1, but you really should be pointing to the CSS2.1 spec in future discussions as it completely supercedes CSS2 which was in general broken at the point of release.

                          – Robin Whittleton
                          Aug 16 '12 at 8:49

















                        Like I said in my answer. ^^ However it does make a difference if you have more of the same type (element, class, id). But that is pretty obvious if 5things say red and 3 say blue well Ima go red.

                        – Sphvn
                        May 11 '10 at 8:47





                        Like I said in my answer. ^^ However it does make a difference if you have more of the same type (element, class, id). But that is pretty obvious if 5things say red and 3 say blue well Ima go red.

                        – Sphvn
                        May 11 '10 at 8:47













                        The wording around specificity probably hasn’t changed much between CSS2 and CSS2.1, but you really should be pointing to the CSS2.1 spec in future discussions as it completely supercedes CSS2 which was in general broken at the point of release.

                        – Robin Whittleton
                        Aug 16 '12 at 8:49





                        The wording around specificity probably hasn’t changed much between CSS2 and CSS2.1, but you really should be pointing to the CSS2.1 spec in future discussions as it completely supercedes CSS2 which was in general broken at the point of release.

                        – Robin Whittleton
                        Aug 16 '12 at 8:49











                        1














                        I would say that:



                        Element < Class < ID


                        I think they only stack into depending what you get if it is multiple of the same.
                        So a Class will always overide the element and ID always over the Class but if it is down to which of 4 elements where 3 is to blue and 1 is to red it will be blue.



                        For Example:



                        .a .b .c .d .e .f .g .h .i .j .k .l
                        {
                        color: red;
                        }

                        .m .n .o
                        {
                        color blue;
                        }


                        Should turn out red.



                        See Example http://jsfiddle.net/RWFWq/



                        "if 5things say red and 3 say blue well Ima go red"






                        share|improve this answer






























                          1














                          I would say that:



                          Element < Class < ID


                          I think they only stack into depending what you get if it is multiple of the same.
                          So a Class will always overide the element and ID always over the Class but if it is down to which of 4 elements where 3 is to blue and 1 is to red it will be blue.



                          For Example:



                          .a .b .c .d .e .f .g .h .i .j .k .l
                          {
                          color: red;
                          }

                          .m .n .o
                          {
                          color blue;
                          }


                          Should turn out red.



                          See Example http://jsfiddle.net/RWFWq/



                          "if 5things say red and 3 say blue well Ima go red"






                          share|improve this answer




























                            1












                            1








                            1







                            I would say that:



                            Element < Class < ID


                            I think they only stack into depending what you get if it is multiple of the same.
                            So a Class will always overide the element and ID always over the Class but if it is down to which of 4 elements where 3 is to blue and 1 is to red it will be blue.



                            For Example:



                            .a .b .c .d .e .f .g .h .i .j .k .l
                            {
                            color: red;
                            }

                            .m .n .o
                            {
                            color blue;
                            }


                            Should turn out red.



                            See Example http://jsfiddle.net/RWFWq/



                            "if 5things say red and 3 say blue well Ima go red"






                            share|improve this answer















                            I would say that:



                            Element < Class < ID


                            I think they only stack into depending what you get if it is multiple of the same.
                            So a Class will always overide the element and ID always over the Class but if it is down to which of 4 elements where 3 is to blue and 1 is to red it will be blue.



                            For Example:



                            .a .b .c .d .e .f .g .h .i .j .k .l
                            {
                            color: red;
                            }

                            .m .n .o
                            {
                            color blue;
                            }


                            Should turn out red.



                            See Example http://jsfiddle.net/RWFWq/



                            "if 5things say red and 3 say blue well Ima go red"







                            share|improve this answer














                            share|improve this answer



                            share|improve this answer








                            edited May 11 '10 at 8:49

























                            answered May 11 '10 at 8:38









                            SphvnSphvn

                            3,08873354




                            3,08873354






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Stack Overflow!


                                • Please be sure to answer the question. Provide details and share your research!

                                But avoid



                                • Asking for help, clarification, or responding to other answers.

                                • Making statements based on opinion; back them up with references or personal experience.


                                To learn more, see our tips on writing great answers.




                                draft saved


                                draft discarded














                                StackExchange.ready(
                                function () {
                                StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f2809024%2fpoints-in-css-specificity%23new-answer', 'question_page');
                                }
                                );

                                Post as a guest















                                Required, but never shown





















































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown

































                                Required, but never shown














                                Required, but never shown












                                Required, but never shown







                                Required, but never shown







                                Popular posts from this blog

                                Tonle Sap (See)

                                I get strange results when I access the Sqlitedatabase with Unity C# via XAMPP

                                Guatemaltekische Davis-Cup-Mannschaft