Alex Rivera | Logout

Can I have an element with an ID that starts with a number?

Asked 2011-04-15T05:56:31.863
53

Can I have an element that has an id that starts with or is completely numbers?

E.g. something like this:

<div id="12"></div>
Edit
Report

1 Answer

7

From the HTML 5 specs...

The id attribute specifies its element's unique identifier (ID). [DOM]

The value must be unique amongst all the IDs in the element's home subtree and must contain at least one character. The value must not contain any space characters.

There are no other restrictions on what form an ID can take; in particular, IDs can consist of just digits, start with a digit, start with an underscore, consist of just punctuation, etc.

An element's unique identifier can be used for a variety of purposes, most notably as a way to link to specific parts of a document using fragment identifiers, as a way to target an element when scripting, and as a way to style a specific element from CSS.

Identifiers are opaque strings. Particular meanings should not be derived from the value of the id attribute.

So... yes :)

From the HTML 4.01 specs...

ID 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 (".").

So... no :(

answered 2011-04-15T06:01:42.203

Your Answer