27

What characters can I use in an identifier for an HTML element?

For example:

<SPAN id="section[5]" ...>

(Or rather, should I stick to certain characters to make sure the id works across all major browsers/JavaScript engine).

Tony the Pony
  • 40,327
  • 71
  • 187
  • 281

6 Answers6

22

In HTML5, the only restrictions are that the ID must be unique within the document, contain at least one character and contain no spaces. See http://www.w3.org/TR/2014/REC-html5-20141028/dom.html#the-id-attribute

As other answers have pointed out, HTML 4 is more restrictive and specifies that

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

Tim Down
  • 318,141
  • 75
  • 454
  • 536
13

In HTML 4, the id attribute holds a NAME token which are defined:

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

As pointed out in a previous post, depending on your implementation you may have troubles with colons and periods along with others: What are valid values for the id attribute in HTML?

Community
  • 1
  • 1
wajiw
  • 12,239
  • 17
  • 54
  • 73
  • 1
    In HTML5, there are fewer restrictions: the ID must be unique within the document, contain at least one character and contain no spaces, and that's it. See http://www.w3.org/TR/html5/dom.html#the-id-attribute – Tim Down Nov 22 '10 at 19:53
  • As @Tim Down said, HTML5 gets rid of most of these restrictions. See http://mathiasbynens.be/notes/html5-id-class, which also explains how to escape weird IDs (or other attribute values) for use in CSS and JavaScript. ([I’ve also made a tool for that.](http://mothereffingcssescapes.com/)) – Mathias Bynens Jul 06 '11 at 06:32
7

From http://www.w3.org/TR/html4/types.html:

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

AndreKR
  • 32,613
  • 18
  • 106
  • 168
  • 1
    HTML5 gets rid of most of these restrictions. See http://mathiasbynens.be/notes/html5-id-class, which also explains how to escape weird IDs (or other attribute values) for use in CSS and JavaScript. ([I’ve also made a tool for that.](http://mothereffingcssescapes.com/)) – Mathias Bynens Jul 06 '11 at 06:33
3

All the above answers + ID must be unique

ajreal
  • 46,720
  • 11
  • 89
  • 119
2

From the HTML4 specification:

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

asthasr
  • 9,125
  • 1
  • 29
  • 43
  • HTML5 gets rid of most of these restrictions. See http://mathiasbynens.be/notes/html5-id-class, which also explains how to escape weird IDs (or other attribute values) for use in CSS and JavaScript. ([I’ve also made a tool for that.](http://mothereffingcssescapes.com/)) – Mathias Bynens Jul 06 '11 at 06:33
0

actually I can successfully use a ° too for example

< span id= "test°" />

Seems to be allowed, and I have no problem with jQuery or other ways to fetch dom elements via the selectors. Perhaps other chars work, but I didn't test them all.