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.
Inline styles added to an element (e.g., style="font-weight: bold;" ) always overwrite any normal styles in author stylesheets, and therefore, can be thought of as having the highest specificity.
Memorize how to calculate specificity! Start at 0, add 100 for each ID value, add 10 for each class value (or pseudo-class or attribute selector), add 1 for each element selector or pseudo-element. Note: Inline style gets a specificity value of 1000, and is always given the highest priority!
The “.” selectors are not unique, same selectors can applicable on multiple elements, if the HTML elements holds the same class property like a list of elements can contains the same class. The “#” are unique.
CSS Selectors and Their “Weights” lowest weight : type and pseudo-element selectors. low weight : class, attribute, and pseudo-class selectors. medium weight : ID selectors. high weight : inline styling.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With