2

Can an id attributes value start with a number?

2
  • why dont u try it urself? what are the results. Commented Nov 3, 2010 at 4:59
  • I was going to post a snarky comment about just writing a basic HTML/XHTML page and validating it yourself on W3C, but then I realised that not everyone knows about the W3C validator. So heres a link :) validator.w3.org Commented Nov 3, 2010 at 5:06

2 Answers 2

5

For HTML, no - http://www.w3.org/TR/html401/types.html#type-name

Theoretically, the collection of legal values in XHTML could be much larger however I doubt it would be compatible with many browsers. See http://www.w3.org/TR/xhtml1/#C_8

Sign up to request clarification or add additional context in comments.

Comments

0

The currently accepted answer is actually incorrect.

All browsers have always supported ID attributes that start with a number, and HTML5 now allows it. See http://mathiasbynens.be/notes/html5-id-class:

The HTML 4.01 spec states that ID 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 (.). For the class attribute, there is no such limitation. Classnames can contain any character, and they don’t have to start with a letter to be valid.

HTML5 gets rid of the additional restrictions on the id attribute. The only requirements left — apart from being unique in the document — are that the value must contain at least one character (can’t be empty), and that it can’t contain any space characters.

Note that you will need to escape the ID attribute value correctly if you want to use it in CSS or JavaScript. See http://mothereffingcssescapes.com/#123-foo.

4 Comments

This question is tagged HTML (the tag is for 4.01). Whilst many vendors implement extra features, to write portable applications, developers should stick to the specification unless there's a vendor specific workaround (such as the -moz / -webkit CSS prefixes).
@Phil 1) Where does it say the HTML tag is for HTML 4.01 only? That would be ridiculous. 2) Did you read my answer? The only reason this got specced in HTML5 is because all browsers have always supported it that way. There’s no reason not to use this if you want to.
The html tag is for 4.01 (look on the tag page). There's a specific tag for html5
@Phil HTML5 is a subset of HTML, just like HTML 4.01 is. They’re not mutually exclusive.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.