id
The id
global attribute defines an identifier (ID) which must be unique in the whole document. Its purpose is to identify the element when linking (using a fragment identifier), scripting, or styling (with CSS).
Warning: This attribute's value is an opaque string: this means that web authors should not rely on it to convey human-readable information (although having your IDs somewhat human-readable can be useful for code comprehension, e.g. consider ticket-18659
versus r45tgfe-freds&$@
).
id
's value must not contain whitespace (spaces, tabs etc.). Browsers treat non-conforming IDs that contain whitespace as if the whitespace is part of the ID. In contrast to the class
attribute, which allows space-separated values, elements can only have one single ID value.
Note: Using characters except ASCII letters, digits, '_'
, '-'
and '.'
may cause compatibility problems, as they weren't allowed in HTML 4. Though this restriction has been lifted in HTML5, an ID should start with a letter for compatibility.
Specifications
Browser compatibility
Desktop | Mobile | |||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|
Chrome | Edge | Firefox | Internet Explorer | Opera | Safari | WebView Android | Chrome Android | Firefox for Android | Opera Android | Safari on IOS | Samsung Internet | |
id |
Yes |
12 |
32
Yes-32
id is a true global attribute only since Firefox 32. |
Yes |
Yes |
Yes |
Yes |
Yes |
32
Yes-32
id is a true global attribute only since Firefox 32. |
Yes |
Yes |
Yes |
See also
- All global attributes.
-
Element.id
that reflects this attribute.
© 2005–2021 MDN contributors.
Licensed under the Creative Commons Attribution-ShareAlike License v2.5 or later.
https://developer.mozilla.org/en-US/docs/Web/HTML/Global_attributes/id