Extreme specificity overriding a CSS ID with 256 chained Classes
The other day Chris Coyier created a test case demonstrating that chaining together 256 classes will give it greater specificity than an id, in theory it shouldn’t. But in IE, Mozilla and WebKit browsers it does, Opera on the other hand upholds the specificity. Not familiar with CSS specificity then take a look at Estelle Weyl’s hilariously informative specifishity chart.
Why does this work?
This got me curious as to why this is the case and thankfully a Mozillian, @heycam, pointed me to the source code of Firefox that shows classes are stored in 8bit strings and if you’re familiar with binary an 8bit string can only hold a maximum value of 255. You guessed it 256 tips this over the edge and overflows it into the ID count giving the classes higher specificity than the ID itself.
@ryanseddon From hg.mozilla.org/mozilla-centra… Gecko overflows the count of classes into the count of IDs, each of which holds 8 bits.— Cameron McCormack (@heycam) August 16, 2012
What does Opera do differently?
According to an Opera employee, @patrick_h_lauke, they store specificity in 16bit strings, so you would need specify 65536 classes names to override an id and show the same behaviour other vendors do. I would be interested in know if using 16bit over 8bit actually impacts the selector engine.
@ryanseddon and to close the loop: yes, opera uses 16 bits instead of 8. bring on 65536 classes…— patrick h. lauke (@patrick_h_lauke) August 16, 2012
mark: WONTFIX
While this is considered an implementation bug it won’t be fixed because it’s such a wildly ridiculous edge case. Another reason why is it could potentially slow down a browser selector engine speed and nobody wants that for crazy edges cases.
An interesting exercise that lets you dig into internals of how browsers work.
Short URL: http://cssn.in/ja/043
Post filed under: css.
Here’s an interesting thing: on chrome you can chain together the same selector 255 times even on a single class element and override the id tag: http://codepen.io/anon/pen/cquBi
Also from the hacker news thread:
WebKit source:
Mozilla source:
If you’re using 256 classes on a single element, I think overriding the CSS ID should be the least of your worries :)
It’s a very interesting find though.
Short example оf 256 classes
Demo: http://codepen.io/anon/pen/yAbci
I wonder what lead to this test-case being crafted. Interesting, nonetheless!
Crazy thing! Thank’s for exploring this cutting edge case. ;-)
This is so cool :)
@Chris –
Thanks Chris!
Using even 65536+ classes in Opera does not break. Demo: http://codepen.io/anon/pen/qxCoG
Also, rendering of this is actually faster in Opera then in Firefox or Chrome (at least on my machine)
I dread to think how this was discovered…….!!!! I really hope someone didn’t stumble across this while debugging why their 256 long class selector was failing to style an element – oh dear!
well done thanks
Awesome discovery, but I’m just fine with it being filed as won’t fix because I can’t think of a real world solution.
I was so baffled when I first saw this Codepen go around, thanks so much for explaining why it’s happening!
I wanted to see if 256 ID selectors would override inline styles, so i wrote this.
http://jsbin.com/igoqej/1/edit
No, no it doesn’t.
(Wierdly enough nothing renders in firefox at all when you get pass 200 levels in the dom tree)
thanks for the wonderful post,
Let’s hope we will not end up having 256 / 65536 in the coming future :))
Hello all,
I am a noob, i don’t know much about css, i am learning css from online blogs, your blog is superb, i have a confusion could you please help, i created this test in which id override class http://codepen.io/anon/pen/fnDqC , In this Id override class.
Jeetendra Singh it is giving id and class the same specificity so whichever appears last will override the other, like if you add 2 different rules for a id/class.