Re: Specificity with id selectors

On Fri, 15 Jun 2001 eric@schwa.com (Eric Costello) wrote:

> Consider the following selectors:
> 
> 	div#myID
> 
> and
> 
> 	#myID
> 
> It is my understanding that by the rules of specificity, the former rule can
> be said to have greater specificity. Is that correct, even though there
> cannot legally exist more than one element with the same id attribute?

   Yes, that's correct.  Consider having those two rules in an external
stylesheet which is being applied to an entire site.  You know that a 'myID'
element will appear on some pages, but not know which element type it will
be.  So you write the latter rule to apply to any 'myID', and the former to
apply to any DIV which is a 'myID' element.  So that's wht this is useful; the
specificity results are as you describe.

> In my tests, browsers ignore a rule with the #myID selector if a rule for
> div#myID also exists anywhere in the document.

   That sounds correct, assuming you were doing simple testing (like setting a
different color for each to see which one wins).  If you were using different
properties and one of the rules got entirely ignored, then you may have found
a bug.  I haven't seen one along those lines, but I'm often surprised by the
persistence of bugs.  (Insert ants-on-melty-watch joke here.)

-- 
Eric A. Meyer                          http://www.meyerweb.com/eric/

Received on Friday, 15 June 2001 14:17:01 UTC