Why do 'foo bar' and 'foo > bar' have the same specificity in CSS?
Asked Answered
I

1

13

I'm curious why using > or other combinators does not affect the specificity of CSS selectors, i.e. why div span (matching a span somewhere inside a div) and div > span (matching a span which is the immediate child of a div) are considered equal regarding the specificity.

I do realize that the usage of combinators is completely irrelevant for the specificity but I wonder if there's a certain reason for it.

Insignificant answered 11/11, 2011 at 16:16 Comment(8)
I'm assuming it's just because they are as specific as each other... "a span that is a descendant of a div" and "a span that is a child of a div", rather than something more specific, such as "a span with some class name that is a child of a div".Tartrazine
Well you could argue that some span inside a div is not as specific as a span that is an immediate child of a divInsignificant
Yeah, very true. Good question!Tartrazine
I can argue that "immediate child" is unnecessarily specific.Neutrality
You linked to the document that explains it...Fahy
@Josh Stodola: Where does that document (or the latest recommended revision for the matter) explain why combinators don't add to specificity?Neutrality
a b is a descendant selector, a > b is a child selector.</pedantics>Idiolect
@zzzzBov: To go even further, there is a special name for them: combinators.Neutrality
S
9

This has actually been brought up in the working group mailing list, way back when, in this thread.

It basically comes down to, yes, intuitively a selector with a combinator looks more specific, but an algorithm, extended form the current one, with this in mind becomes much more complicated than the "simple" triplets used now, which is pretty confusing for people as it is.

Finally,

While this could have been the case, this is one of the few things in CSS2
that have been interoperably implemented for years, and therefore won't
change in CSS2.1.

"If it ain't broke, don't fix it." seemed to be the final call.

Sate answered 11/11, 2011 at 16:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.