W3C home > Mailing lists > Public > www-style@w3.org > June 2001

Re: Specificity with id selectors

From: Eric A. Meyer <emeyer@theopalgroup.com>
Date: Fri, 15 Jun 2001 14:15:49 -0400 (EDT)
To: www-style@w3.org
Message-ID: <Pine.LNX.4.21.0106151412070.2944-100000@penguin.theopalgroup.com>
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 GMT

This archive was generated by hypermail 2.2.0+W3C-0.50 : Monday, 27 April 2009 13:54:09 GMT