When creating the id
attributes for HTML elements, what rules are there for the value?
相关问题
- Views base64 encoded blob in HTML with PHP
- Is there a way to play audio on a mobile browser w
- HTML form is not sending $_POST values
- implementing html5 drag and drop photos with knock
-
Why does the box-shadow property not apply to a
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
Selectors in jQuery API (see bottom note)
HTML5: Permitted Values for ID & Class Attributes
As 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, ID values must begin with a letter, which can then be followed only by letters, digits, hyphens, underscores, colons and periods.
In HTML5 these are valid:
Just bear in mind that using numbers, punctuation or special characters in the value of an ID may cause trouble in other contexts (e.g., CSS, JavaScript, regex).
For example, the following ID is valid in HTML5:
However, it is invalid in CSS:
From the CSS2.1 spec:
In most cases you may be able to escape characters in contexts where they have restrictions or special meaning.
W3C References
HTML5
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.
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:
-
(hyphen),_
(underscore),:
(colon) and.
(period)but one should avoid
:
and.
beacause: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.
alphabets-> caps & small
digits-> 0-9
special chars-> ':', '-', '_', '.'
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 '_'.
Also, spaces are not allowed, if provided, they are treated as different values, which is not valid in case of the id attributes.
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.
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
<div id="first.name"></div>
is valid. You can select that element in CSS as#first\.name
and in jQuery like so:$('#first\\.name').
But if you forget the backslash,$('#first.name')
, you will have a perfectly valid selector looking for an element with idfirst
and also having classname
. This is a bug that is easy to overlook. You might be happier in the long run choosing the idfirst-name
(a hyphen rather than a period), instead.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
firstName
orFirstName
?" because you will always know that you should typefirst_name
. Prefer camel case? Then limit yourself to that, no hyphens or underscores, and always, consistently use either upper-case or lower-case for the first character, don't mix them.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
id="firstName"
in your HTML (lower-case 'f') and#FirstName { color: red }
in your CSS (upper-case 'F'), that buggy browser would have failed to set the element's color to red. At the time of this edit, April 2015, I hope you aren't being asked to support Netscape 6. Consider this a historical footnote.