When creating the id
attributes for HTML elements, what rules are there for the value?
For HTML 4, the answer is technically:
HTML 5 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters. The id attribute is case sensitive in XHTML [citation needed]. As a purely practical matter, you may want to avoid certain characters. Periods, colons and '#' have special meaning in CSS selectors, so you will have to escape those characters using a backslash in CSS or a double backslash in a selector string passed to jQuery. Think about how often you will have to escape a character in your stylesheets or code before you go crazy with periods and colons in ids. For example, the HTML declaration You can simplify your development tasks by strictly sticking to a naming convention. For example, if you limit yourself entirely to lower-case characters and always separate words with either hyphens or underscores (but not both, pick one and never use the other), then you have an easy-to-remember pattern. You will never wonder "was it A now very obscure problem was that at least one browser, Netscape 6, incorrectly treated id attribute values as case-sensitive. That meant that if you had typed |
|||||||||||||||||||||
|
From the HTML 4 specification:
A common mistake is to use an ID that starts with a digit. |
|||||||||||||||||||||
|
You can technically use colons and periods in id/name attributes, but I would strongly suggest avoiding both. In CSS (and several JavaScript libraries like jQuery), both the period and the colon have special meaning and you will run into problems if you're not careful. Periods are class selectors and colons are pseudo-selectors (eg., ":hover" for an element when the mouse is over it). If you give an element the id "my.cool:thing", your CSS selector will look like this:
Which is really saying, "the element with an id of 'my', a class of 'cool' and the 'thing' pseudo-selector" in CSS-speak. Stick to A-Z of any case, numbers, underscores and hyphens. And as said above, make sure your ids are unique. That should be your first concern. |
|||||||||||||||||||||
|
jQuery does handle any valid ID name. You just need to escape metacharacters (i.e., dots, semicolons, square brackets...). It's like saying that JavaScript has a problem with quotes only because you can't write
|
|||
|
Strictly it should match
But jquery seems to have problems with colons so it might be better to avoid them. |
|||||||||
|
In practice many sites use The HTML 5 draft specification loosens up the rules for the |
|||
|
HTML5:gets rid of the additional restrictions on the id attribute see here. The only requirements left (apart from being unique in the document) are:
PRE-HTML5:ID should match:
but one should avoid For example, an ID could be labelled "a.b:c" and referenced in the style sheet as #a.b:c but as well as being the id for the element, it could mean id "a", class "b", pseudo-selector "c". Best to avoid the confusion and stay away from using . and : altogether. |
||||
|
Hyphens, underscores, periods, colons, numbers and letters are all valid for use with CSS and JQuery. The following should work but it must be unique throughout the page and also must start with a letter [A-Za-z]. Working with colons and periods needs a bit more work but you can do it as the following example shows.
|
|||
|
HTML5Keeping in mind that ID must be unique, ie. there must not be multiple elements in a document that have the same id value. The rules about ID content in HTML5 are (apart from being unique):
This is the W3 spec about ID (från MDN):
More info: |
||||
|
To reference an id with a period in it you need to use a backslash. Not sure if its the same for hyphens or underscores. For example: HTML
CSS
|
|||||
|
From the HTML 4 spec... 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 ("."). EDIT: d'oh! Beaten to the button, again! |
|||||
|
It appears that although colons (:) and periods (.) are valid in the HTML spec, they are invalid as id selectors in CSS so probably best avoided if you intend to use them for that purpose. |
|||||||||
|
Also, never forget that an ID is unique. Once used, the ID value may not appear again anywhere in the document. You may have many ID's, but all must have a unique value. On the other hand, there is the class-element. Just like ID, it can appear many times, but the value may be used over and over again. |
||||
|
HTML5 ID Attribute ValuesAs of HTML5, the only restrictions on the value of an ID are:
Similar rules apply to classes (except for the uniqueness, of course). So the value can be all digits, just one digit, just punctuation characters, include special characters, whatever. Just no whitespace. This is very different from HTML4. In HTML 4, In HTML5 these are valid:
Just bear in mind that using punctuation and special characters in the value of an |
||||
|
|
||||
|
for
At least one character, no spaces. This opens the door for valid use cases such as using accented characters. It also gives us plenty of more ammo to shoot ourselves in the foot with, since you can now use id values that will cause problems with both CSS and JavaScript unless you’re really careful. |
|||
|
Older versions of Netscape had problems with _ in names/elements, so I've stuck to A-Z, a-z, 0-9 and "-" in my IDs out of habit. I'd stretch to _:s, but I haven't had any real reason to use them. Shrugs |
||||
|
alphabets-> caps & small the format should be either starting from '.' or an alphabet, followed by either of the special chars of more alphabets or numbers. the value of the id field must not end at an '_'.
|
|||
|
In HTML5, an id can't start with a number, e.g. id- |
|||||
|
You can use big and small english alphabet letters (A-Z & a-z), numbers (0-9), you can't start with numbers, no space, everything else is not allowed. |
|||
|
protected by Bill the Lizard Sep 12 '12 at 12:43
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site.
Would you like to answer one of these unanswered questions instead?
ID
values are very different. Here's a quick and complete rundown of HTML5ID
rules: stackoverflow.com/a/31773673/3597276 – Michael_B Aug 3 '15 at 0:32**.**)
with jQuery will run you into quite a bit of trouble, for example, using<input id="me.name" />
and then$("#me.name").val()
will cause jQuery to look for a<me>
tag with the class.name
, which no one wants really! – Sam Swift 웃 Aug 26 '15 at 15:20