By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
432,245 Members | 884 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 432,245 IT Pros & Developers. It's quick & easy.

Calculating CSS Selector Specificity

P: n/a
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-spec....blogspot.com/

Comments and/or criticisms welcome.

Aug 25 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
sa***@fendoo.com wrote:
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-spec....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.
Aug 25 '06 #2

P: n/a
sa***@fendoo.com wrote:
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

Aug 26 '06 #3

P: n/a
On 25 Aug 2006 00:58:41 -0700, sa***@fendoo.com wrote:
>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-spec....blogspot.com/

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.

--
Stephen Poley

http://www.xs4all.nl/~sbpoley/webmatters/
Aug 26 '06 #4

P: n/a
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.
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

Aug 28 '06 #5

P: n/a
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.

Aug 28 '06 #6

P: n/a
sa***@fendoo.com wrote:
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.

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.
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.
Aug 28 '06 #7

P: n/a
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.

Aug 28 '06 #8

P: n/a
sa***@fendoo.com wrote:
>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.
Aug 28 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.