September 21, 2019
Ids are everywhere. But what are the rules for them?
Turns out, the rules in HTML5 are pretty permissive: unique, at least one character, and no spaces.1
From the spec itself:2
When specified on HTML elements , the id attribute value must be unique amongst all the IDs in the element’s tree and must contain at least one character. The value must not contain any ASCII whitespace.
The rules used to be more specific. In HTML43:
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 (”.”).
Note the restrictions as well as the fact that the id had to start with a letter.
Thanks for reading! My name's Stephen Weiss. I live in Chicago with my wife, Kate, and dog, Finn.
Click here to see the archives of my weeks in review and sign up yourself!