The id attribute is a global attribute that gives an element a unique identifier in the document. Use it for fragment links (href="#…"), CSS (#id), form labels (for), and scripts (getElementById).
Unlike class, an element has at most one id, and that value must not be reused on another element in the same page.
Baseline · Widely available.
Not experimental or deprecated. Supported everywhere as a core HTML global attribute.
Warning — avoid window.idName.
MDN: do not rely on ids as global window properties. Prefer document.getElementById() or querySelector() to avoid clashes with browser APIs.
Syntax
HTML Syntax
syntax.html
<element id="unique-name">
unique-name must be unique in the document and must not contain ASCII whitespace.
Values
Attribute Values
One token per element. Keep names simple so CSS and JS stay easy:
Value
Description
Try it
unique-name
Any string without ASCII whitespace; must be unique in the document
Open Try It Yourself to click the button and see getElementById update the text.
How It Works
Stable hooks.status and go mark the paragraph and button.
Look up by id.getElementById("go") returns that one element.
Update the target. On click, change the text of the status element.
Prefer this API. Safer than reading window.go or window.status.
Edge Cases & Pitfalls
Most id bugs come from duplicates, spaces, or awkward characters in selectors.
unique
Never duplicate an id
Browsers may return the first match only. CSS, scripts, and assistive tech become unreliable.
syntax
No ASCII whitespace
Unlike class, spaces are not separators. Keep a single token such as main-nav.
css
Prefer valid CSS identifiers
Values like 1234 or my?id need escaping in #… selectors. Use plain names when you can.
js
Skip window[id] shortcuts
Global id properties can collide with future browser APIs. Use getElementById or querySelector.
vs class
Styling many elements? Use class
Reserve id for unique hooks (labels, fragments, one-off scripts). Shared look and feel belongs on class.
Compatibility
Browser Support
id is an HTML global attribute in the browser. It is not a Node.js, Deno, or Bun API.
✓ Baseline · Widely available
id
Core HTML feature with full support across modern and legacy browsers.
100%Global support (attribute)
Google ChromeAll
Full support
Mozilla FirefoxAll
Full support
Apple SafariAll
Full support
Microsoft EdgeAll
Full support
OperaAll
Full support
Samsung InternetAll
Full support
Internet ExplorerAll
Full support
idWidely supported
Bottom line: Safe to use everywhere. Keep values unique and whitespace-free.
Remember
Key Takeaways
Unique: one id value per document, one id per element.
Uses: fragments, labels, CSS #id, and getElementById.
Names: no spaces; prefer simple CSS-safe identifiers.
Avoid: duplicates and window.id shortcuts.
One line:id names one element uniquely — for links, labels, CSS, and scripts.
Frequently Asked Questions
id gives an element a unique name in the document. Use it for fragment links (#id), CSS (#id), label for pairing, and JavaScript (getElementById or querySelector).
No. Each id must be unique in the whole document. Duplicates break CSS, scripts, and accessibility in unpredictable ways.
id identifies one element and must be unique. class can appear on many elements and is meant for shared styling or grouping.
An id must not contain ASCII whitespace. Prefer simple CSS identifiers (letters, digits, hyphen, underscore) so selectors need no escaping.
No. MDN warns that treating ids as window properties can clash with browser APIs. Prefer document.getElementById() or querySelector().
id is a global attribute. Almost every HTML element can have one.