If you find this helpful, please click the Google Button to the left, if it is white, to make it turn blue or red. Thank you! (It also helps find this page again more easily.) |
The id
attribute in HTML
The id
attribute is a unique identifier for an element in an HTML document.
Rules for coding an id
attribute
- The
id
attribute is one of the global attributes that may be included on any HTML element. - The value of the
id
attribute cannot be an empty string and is not permitted to have any spaces within it. - The same
id
attribute value cannot be used for any otherid
in the same HTML document. It must be unique. - For backward compatibility, most browsers will treat the deprecated
<a name>
attribute on elements where it is not supposed to be coded as if it were anid
attribute. Since the value of theid
attribute must be unique, the same value should never be used for any otherid
orname
attribute within the same document.
Rules for referencing a fragment identifier
When an id
value is referenced in a URL attribute such as <a href>, it must be URL encoded along with the rest of the URL. Use percent escape codes as explained in the URL-encoding tutorial for any special characters in the URL, including the fragment identifier. For example, when the fragment identifier contains a hash character (#
), which is common for podcast episodes in feeds or HTML transformed from a feed by a style sheet it must be encoded as %23
.
Here is an example HTML link that references a fragment identifier with a #
character percent-encoded character as %23
in the link.
<a href="http://www.HTML-5.com/attributes/id-attribute.html#%2355">Episode #55</a>