Recent Posts

Showing posts with label id. Show all posts
Showing posts with label id. Show all posts

Valid values for id attribute in HTML

For HTML 4, the answer is technically:

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

HTML 5 is even more permissive, saying only that an id must contain at least one character and may not contain any space characters.

However, as a practical matter, you will be somewhat more limited if you want your documents to work with a variety of browsers, CSS editors, and JavaScript frameworks.

As noted in other responses, jQuery has problems with ids that contain periods and colons.

A more subtle problem is that some browsers treat id attribute values as case-sensitive, while other browsers do not. That means that if you type id="firstName" in your HTML (lower-case 'f') and #FirstName { color: red } in your CSS (upper-case 'F'), a buggy browser will set the element's color to red. Because both definitions use valid characters for the id, you will receive no error from a validation tool.

You can avoid these problems 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 or FirstName?" because you will always know that you should type first_name