thanks for the samples, can hopefully explain more now,
so specificty is the reason why a single ID will override any amount elements and classes which is why they, ID's are so important
yes IE used to have an issue with chaining id's and classes so that may have had a bearing as to why this was not used properly as IE6 is still floating about I believe ;)
but the essence is .. to use 2 of swa's examples with a slight change
.menu li.myclass img {...} is 0,0,2,2 - (2 classes, 2 elements)
#myid img {...} is 0,1,0,1 - (1 ID, 0 classes, 1 element)
if that pertains to
<ul id="myid" class="menu myclass red green bold whatever">
<li class="myclass"><img src="#" /></li>
</ul>
the first CSS rule
.menu li.myclass img {}
has 2 elements (li and img) and 2 classes (menu and myclass) in it's selector so its pretty specific - now if it's setting the background-color of the image to pink then it's likely to be taken.. the selector reads from right to left as true, it matches , it has an ancestor list element with a class of "myclass" which hsubsequently has an ancestor with the class of "menu" so the background color will be applied
but the second rule
#myid img {}
no matter where in the cascade it appears it will override the first, that single ID is enough to override 10 million classes and elements
even if you chain all the classes together for
.menu li.myclass img {...}
so that selector becomes:
.menu.myclass.red.green.bold.whatever li.myclass img{}
the specificity of that selector then becomes:
0,0,7,2 (7 classes and still 2 elements) - hehe, the image is pretty sure to have a pink background now ;)
but let's bring back the ID..
#myid img {}
it's specificity is
0,1,0,1 (1 ID, 0 classes and 1 element)
in simple terms 101 beats 72 (lack of commas is deliberate)
BUT that's not the way to see it even though it works most times, rarely are there more than 9 classes chained ;), specificity is not base 10, it's not even base 16 - it's an unknown base if you like. The commas separate the columns and the columns order of importance is the same as CSS they're read right to left, he further left you get the more important!
You could , for example, have 300 classes on your element and chain them all together like the
0,0,7,2 example - if you did that the selector would the have a specificity of
0,0,300,2.. but still the
0,1,0,1 would beat it, thanks to the ID, the ID puts a one in the "hundreds" (3rd from right) column which beats the 0 - the 300 and the 0 in the "tens" (2nd from right) column are negated as soon as there are any ID's in the race..
btw the fourth from right column is for an inline style
<ul id="myid" class="menu myclass red green bold whatever">
<li class="myclass"><img src="#" style="background: red"> /></li>
</ul>
it will trump even an ID, as it will put a 1 the fourth from right column for that particular img the background will be red, and the specificty is
1,0,0,0 .. no selector beats that, no matter how many classes or ID's chained ;) - that's why inline styles should be avoided unless they're being added or changed by JS in which case the JS can also take care of the styling!