Working on a project, nearly finished and just tidying up the HTML and I find out that you're not really allowed to have an ID that is just a number-
< a> attribute "id" has invalid value "567" The attribute ID is of type ID. As described above, it should begin with a letter and have no spaces
Good <a id="567" href="/index.html"> Good <a id="n567" href="/index.html">
I can go through my code and add a letter then strip it when the value is used in my jQuery but it would be messing around I don't really need.
Is there a reason I shouldn't be using numbers as ID's?
Rules for Using the ID Attribute The ID must start with a letter (a-z or A-Z). All subsequent characters can be letters, numbers (0-9), hyphens (-), underscores (_), colons (:), and periods (.). Each ID must be unique within the document.
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 ("."). Show activity on this post. Because that's what the HTML4 spec dictates.
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.
In CSS, identifiers (including element names, classes, and IDs in selectors) can contain only the characters [a-zA-Z0-9] and ISO 10646 characters U+00A0 and higher, plus the hyphen (-) and the underscore (_); they cannot start with a digit, two hyphens, or a hyphen followed by a digit.
That's just what the spec says.
From the HTML 4 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 (".").
The good news is that the HTML 5 specification is more lenient:
The
id
attribute specifies its element's unique identifier (ID). 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.
Why can't I have a numeric value as the ID of an element?
Because that's what the HTML4 spec dictates.
On the other hand, the HTML5 spec has removed this requirement.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With