Naming id’s with numbers

Date: 10 April 2008
Author: Russ Weakley

I was recently sent this question:

I am trying to apply a div to a page and it will not show when I use the name “420wide” but it will if I use “wide420″. Why is this? Is this rule documented somewhere?

The simple answer is that the ID name must be unique in a document and it must begin with a letter – not a number.

You can find out more in the W3C’s HTML specification relating to the id attribute:

id = name [CS]
This attribute assigns a name to an element. This name must be unique in a document.

http://www.w3.org/TR/html4/struct/global.html#adef-id

There are two associated links with this info:

ID and NAME tokens must begin with a letter ([A-Za-z]) and may be followed by any number of letters, digits ([0-9]), hyphens (“-”), underscores (“_”), colons (“:”), and periods (“.”).

http://www.w3.org/TR/html4/types.html#type-name

CS – The value is case-sensitive (i.e., user agents interpret “a” and “A” differently).

http://www.w3.org/TR/html4/types.html#case-sensitive

Comments so far

  1. rb says:

    It never ceases to amaze me how many people don’t know these fundamental rules (especially the fact that id’s must be unique per page) when they have always been clearly defined at w3c.

  2. thank you for this Article

    Great Point

  3. ok, new checklist item to add my development process. Thanks for this. :)

  4. Hugo says:

    I already noticed that, but I haven’t consulted the reference to check it. And reading this made me remember that experience.
    Thanks for remembering me of this.

  5. Jermayn says:

    Well rb not everyone knows the rules like you, a lot of designers are learning as they go and you cannot expect them to know. But its good that you…

    Thanks for the tip Russ, as I know myself I have made this mistake and im sure many others have as well….

  6. Ahmad Alfy says:

    ABC of CSS!
    Basics are more important than anything!

  7. [...] Algo que deberías saber: Cuando trabajes con XHTML/CSS las id’s deben empezar con una letra (A-Z / a-z) y no un número. El segundo caracter si puede ser un número (un guión, un punto, etc.). En caso de que esta regla no sea respestada, no se reconocerá la id. [...]

  8. Thanks for this “mise au point”