CSS Specificity Wars

Rate this:
By Fulco (Contact - View My Woyano)
Published Wed 01 Aug 2007, 246 Views, 3 Comments

From Stuff and Nonsense



The Dark Side

My problem was a simple one, how to feed a transparent PNG image to browsers which support transparency and a GIF image to older browsers which don't, without resorting to hacks. Here's the markup,

<div id="nav-supp">
<p><a id="a-02" href="#webstandards-org">Top</a></p>
<!-- etc. -->
</div>

and my CSS starting point.

a#a-02 { background-image : url(n.gif); }
a[id="a-02"] { background-image : url(n.png); }

I had assumed that a modern browser would see and apply both rules (with the second overriding the first) and that an older browser which does not understand attribute selectors would see and apply only the first, ignoring the second. I was wrong. Modern browsers did not apply the PNG image as I expected. The reason? A standard id selector wins over an attribute selector in terms of the cascade. Dagnammit! I know I should have read the specs, but somehow that particular pleasure had escaped me. If I had, I might have learned that;

ID selectors have a higher specificity than attribute selectors. For example, in HTML, the selector #p123 is more specific than [id=p123] in terms of the cascade.

Sith Lords

A little Googling uncovered some rather dry reading on the subject of selector specificity (resources below).

First, let's look back at what Lord Elasticus (Patrick Griffiths) wrote on the subject of specificity (with one or two minor changes to fit our nefarious purpose).

You give every id selector ("#whatever") a value of 100, every class selector (".whatever") a value of 10 and every HTML selector ("whatever") a value of 1. Then you add them all up and hey presto, you have the specificity value.

  • p has a specificity of 1 (1 HTML selector)
  • div p has a specificity of 2 (2 HTML selectors; 1+1)
  • .sith has a specificity of 10 (1 class selector)
  • div p.sith has a specificity of 12 (2 HTML selectors and a class selector; 1+1+10)
  • #sith has a specificity of 100 (1 id selector)
  • body #darkside .sith p has a specificity of 112 (HTML selector, id selector, class selector, HTML selector; 1+100+10+1)

If all of these examples were used, div p.sith (with a specificity of 12) would win out over div p (with a specificity of 2) and body #darkside .sith p would win out over all of them, regardless of the order.

Darth (Gez) Lemon quotes the W3C.

A selector's specificity is calculated as follows:

  • count the number of ID attributes in the selector (= a)
  • count the number of other attributes and pseudo-classes in the selector (= b)
  • count the number of element names in the selector (= c)
  • ignore pseudo-elements.

Concatenating the three numbers a-b-c (in a number system with a large base) gives the specificity.

Too much! For me, the W3C really is in a galaxy far, far away!

Is it possible to learn these powers?

Math was never my strong point, so to help me understand calculating specificity better I made a chart based on the following specificity (or Sith power) values (Ed says: Ignoring inline styles and !important).


element selector

class selector

id selector
Sith power (specificity): 0, 0, 1
(1)
Sith power (specificity): 0, 1, 0
(10)
Sith power (specificity): 1, 0, 0
(100)

Each character (selector) is given its own Sith power (specificity value) depending on how powerful they are in the ways of the Dark Side. A storm trooper is less powerful than Vader who is in turn less powerful than the Emperor.

CSS: Specificity WarsCSS: Specificity Wars: (jpg, 142Kb)


This Item
Category: Knowledge, Snippets, General
Tags: css, web,design
Share it
Link to this item:
Bookmark this item: RSS Feed

People who liked this item

    3 Comments

  1.  
    jfmuk ~ 16 months ago
    0 votes thumbs up thumbs down
    Dude, I have no idea what you're talking about but I like the way you think. Mabye you could apply your practical visualisation of abstract concepts to the global economy? You might then be able to present it in a format that Bush might understand!
    [ reply ]
    1.  
      jfmuk ~ 16 months ago
      0 votes thumbs up thumbs down
      p.s. if you're going to simplify the global economy, ensure that Bush is represented by Ja Ja Binx
      [ reply ]
      1.  
        dreamz ~ 16 months ago
        0 votes thumbs up thumbs down
        Well done!!
        [ reply ]
        1.  
          22 votes thumbs up thumbs down
          This is my two cents...

             
          Hey you know AdGuy always gets the last word! ;)

        Please Login to Add Your Comment   ..or..  

        Replying to comment by