Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion GroupsGeneralPHPASPPerlColdFusionFlashHTML, CSS, ScriptsBrowsers

Webmaster Forum / HTML, CSS, Scripts / CSS / August 2006



Tip: Looking for answers? Try searching our database.

Calculating CSS Selector Specificity

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
sajid@fendoo.com - 25 Aug 2006 08:58 GMT
The CSS 2.1 Specification describes how to sort a list of selectors in
order of specificity, but it doesn't provide a method to calculate the
specificity of a single selector in isolation.

I've devised a method to do this, which I describe in the following
article:

http://calculating-css-selector-specificity.blogspot.com/

Comments and/or criticisms welcome.
Harlan Messinger - 25 Aug 2006 15:34 GMT
> The CSS 2.1 Specification describes how to sort a list of selectors in
> order of specificity, but it doesn't provide a method to calculate the
> specificity of a single selector in isolation.

Of course they do. www.w3.org/TR/CSS2/cascade.html#specificity

> I've devised a method to do this, which I describe in the following
> article:
>
> http://calculating-css-selector-specificity.blogspot.com/

In which you wrote, "A method currently popular is to simply concatenate
the numbers a, b, c and d into a single decimal number

Selector    Specificity    Specificity Value
p#title.red    0, 1, 1, 1    0111 = 111

But this method fails if one of b, c, or d is greater than 9."

This demonstrates that you overlooked their examples, in which they did
exactly this--concatenating the numbers, treating them as digits base
10--and the part where they wrote "in a number system with a large
base", which deals head-on with the case where any of the numbers is
greater than 9. In other words, this isn't a "method currently popular".
It's the way the computation was defined.
sajid@fendoo.com - 28 Aug 2006 19:44 GMT
> But this method fails if one of b, c, or d is greater than 9."
>
[quoted text clipped - 4 lines]
> greater than 9. In other words, this isn't a "method currently popular".
> It's the way the computation was defined.

Harlan, you are refering to the CSS 2 specification.

The CSS 2.1 Specification switched to defining the specificity of a
selector as comma-delimited quad of integers for the precise reason I
give.

Follow the linke below for more information:

http://meyerweb.com/eric/css/link-specificity.html
Harlan Messinger - 28 Aug 2006 20:04 GMT
>  > But this method fails if one of b, c, or d is greater than 9."
>> This demonstrates that you overlooked their examples, in which they did
[quoted text clipped - 9 lines]
> selector as comma-delimited quad of integers for the precise reason I
> give.

Hmm, don't know why my link wasn't updated. It doesn't matter, though.
What you wrote is what 2.0 had, and I figure they dropped it from 2.1
because while it was meant to help explain it, it probably just caused
more confusion, and for no purpose, because as has been pointed out to
you, an absolute number doesn't mean anything.

How do you compare numbers you see on paper? How do you alphabetize
words? By exactly the same process used in the spec for the
comma-separated numbers. The only difference between the comma-separated
convention and the rendering of a number to some base is the presence or
absence of the commas! Conceptualizing the specificity as an absolute
number rather than as a set of values adds nothing.
VidTheKid - 26 Aug 2006 17:06 GMT
> The CSS 2.1 Specification describes how to sort a list of selectors in
> order of specificity, but it doesn't provide a method to calculate the
> specificity of a single selector in isolation.

Yes it does, although it doesn't exactly result in a "number" in the
strictest mathematical or physical sense, considering its base might as
well be infinity. And why is such a calculation important?  Why would
anyone need to know how specific a selector is unless they need to know
whether it "wins" over another selector?  That's kind of like trying to
measure the "absolute voltage" of something -- voltage is always
measured as a difference in potential between two points.

--
Vid the Kid
sajid@fendoo.com - 28 Aug 2006 20:01 GMT
> Yes it does, although it doesn't exactly result in a "number" in the
> strictest mathematical or physical sense, considering its base might as
> well be infinity.

You're right, it does.

But my point is precisely the fact that the definition doesn't result
in a number.

> And why is such a calculation important?  Why would
> anyone need to know how specific a selector is unless they need to know
> whether it "wins" over another selector?

I don't know if it's important or not.

All I'm saying is: here's a way to do it if you think you'll find it
useful.

>That's kind of like trying to
> measure the "absolute voltage" of something -- voltage is always
> measured as a difference in potential between two points.

Voltage and other physical units may be relative but specificity isn't.

My article demonstrates this precise fact !

viz. Specificity is more like the abstract concept of number, you can
refer to the  absolute specificity of a selector in the same sense that
you can refer to the absolute size of a number.
Stephen Poley - 26 Aug 2006 20:33 GMT
>The CSS 2.1 Specification describes how to sort a list of selectors in
>order of specificity, but it doesn't provide a method to calculate the
[quoted text clipped - 6 lines]
>
>Comments and/or criticisms welcome.

What do you plan to do with these numbers when you have calculated them?
The "specificity of a single selector in isolation" appears to be a
meaningless concept. A single selector in isolation will always be
applied.

Signature

Stephen Poley

http://www.xs4all.nl/~sbpoley/webmatters/

sajid@fendoo.com - 28 Aug 2006 20:07 GMT
> What do you plan to do with these numbers when you have calculated them?

I don't know.

It's just something I'm placing in the public domain which other people
might find useful.

> The "specificity of a single selector in isolation" appears to be a
> meaningless concept. A single selector in isolation will always be
> applied.

It's not a meaningless concept because semantically it is identical to
the comma-delimited quad notation.

Whether it's a useful concept or not, I don't know.

The posters so far in this thread (unanimously) do not seem to think
it's useful concept but maybe other people might.
Harlan Messinger - 28 Aug 2006 20:13 GMT
>> What do you plan to do with these numbers when you have calculated them?
>
> I don't know.
>
> It's just something I'm placing in the public domain which other people
> might find useful.

W3 placed it in the public domain years ago.
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.