#id#id : Repeated occurrences of the same simple selector should increase specificity but don't for IDs in IE9
Asked Answered
G

1

4

For some time now I'm using a little trick that I thought was smart.

That is combining the same css selector to add specificity to the rule's selector.

CSS Specs do mention :

Note: Repeated occurrances of the same simple selector are allowed and do increase specificity.

http://www.w3.org/TR/css3-selectors/#specificity

For example if HTML is

<body>
    <section id="main">
        <header class="titles">
            <h2>Title red</h2>
            <h2 class="blue">Title blue</h2>
        </header>
        <h2 class="blue">Title blue</h2>
    </section>
</body>

And CSS

#main .titles h2{
    color: red;
}
#main .blue.blue{
    color: blue;
}

This way I can use the class .blue to override styles, event in the header...

(I'm doing this because I hate using !important. To me it should be avoided at all costs.)

First selector weighs 0111 (1 id, 1 class, 1 element) Second selector weighs 0120 (1 id, 2 classes)

Sometimes I do it with IDs. And it works... in real browsers... This selector :

#main#main .blue{}

should weigh 0200, as it's got 2 IDs right?

Well IE9 (didn't try others) does not interpret multiple identical IDs in selectors. This selector won't override #main .titles h2{} in IE9...

IE's css console shows a computed selector equal to #main .blue and removes the second occurence...

Why is that?

To me this is just another IE implementation "bug".

As @BoltClock suggested, I filed a report here :

https://connect.microsoft.com/IE/feedbackdetail/view/958790/repeated-occurrences-of-the-same-simple-selector-should-increase-specificity-even-with-ids

Glorification answered 29/8, 2014 at 9:58 Comment(7)
I can confirm, this is weird CSS: #body#body .blue{}Stpeter
Do you mean #body #body .blue{} ?Sedate
I don't think this is intended behaviour, so while it works right now, it might be changed in the future so your webpage will suddenly look different. If you want to override a style just use !important.Postmaster
@MichaëlvandeWeerd See specs : w3.org/TR/css3-selectors/#specificity it mentions "Repeated occurrances of the same simple selector are allowed and do increase specificity."Glorification
@ArmelLarcier I stand corrected.Postmaster
@MichaëlvandeWeerd It seems it was unclear for a long time before it was specified in 2009... lists.w3.org/Archives/Public/www-style/2009Oct/0237.htmlGlorification
I have tested your .blue.blue version in IE11 and it is working as expected. That said the #main#main does not so I have updated your bug with a repro to show the disparity between the two.Myongmyopia
B
4

Yes, judging by the behavior shown in F12, this is definitely a bug. It's also a violation of the spec, if you interpret "do increase specificity" as "must increase specificity". This issue seems to only affect ID selectors. Class selectors, attribute selectors and pseudo-classes are OK.

This appears to have been reported before as when I search Microsoft Connect, it turns up an existing report, but I can't view it for some reason. The issue is still present in IE11; if you can't view the report either, feel free to file another one.

Bryant answered 29/8, 2014 at 11:31 Comment(2)
Thanks! I secretly hoped YOU would answer this ;-) Report filed... connect.microsoft.com/IE/feedbackdetail/view/958790/…Glorification
@Armel Larcier: Haha it's my pleasure ;)Bryant

© 2022 - 2024 — McMap. All rights reserved.