HTML id Attribute

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

Definition and Usage

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.

HTML Syntax

syntax.html
<element id=" unique-name">

unique-name must be unique in the document and must not contain ASCII whitespace.

Attribute Values

One token per element. Keep names simple so CSS and JS stay easy:

ValueDescriptionTry it
unique-nameAny string without ASCII whitespace; must be unique in the documentTry it
section-nameCommon for page landmarks used with href="#…"Try it
control-namePairs with <label for="…"> on form controlsTry it
Valid CSS identifier (recommended)Letters, digits, -, _ — no leading digit if you use #id without escapingTry it

Live Preview

Type an id, apply it to the sample box, and see the matching fragment link markup.

Whitespace is removed on apply so the demo stays a valid single id token.

Preview

Jump to target

Target box — current id: intro

<a href="#intro">Jump to target</a>
<p id="intro">…</p>

Fragment link points to #intro.

HTML Examples

Short, copy-ready snippets. Use Try It Yourself for a live editor.

Example 1 — Fragment Link

Jump within the page using href="#id" and a matching element id.

HTML
<p><a href="#footer">Jump to footer</a></p>

<main id="main-content">
  <p>Main content goes here.</p>
</main>

<footer id="footer">
  <p>&copy; 2026 Example Site</p>
</footer>
Try It Yourself

How It Works

  1. Give a target. Put a unique id on the destination element (here, footer).
  2. Link with a hash. href="#footer" tells the browser to scroll to that id.
  3. One id each. main-content and footer must not be reused elsewhere on the page.
  4. URL fragment. Opening page.html#footer jumps to the same target.

Example 2 — Label Linked with for + id

Connect a label to a control so clicking the text focuses the input.

HTML
<label for="username">Username</label>
<input type="text" id="username" name="username" autocomplete="username">
Try It Yourself

How It Works

  1. id on the control. The input gets id="username".
  2. for on the label. for="username" must match that id exactly.
  3. Larger click target. Clicking the label focuses (or activates) the control.
  4. Accessibility. Screen readers announce the label with the field.

Example 3 — Select with getElementById

Find one element by its unique id in JavaScript.

HTML
<p id="status">Ready.</p>
<button type="button" id="go">Update status</button>

<script>
  document.getElementById("go").addEventListener("click", function () {
    document.getElementById("status").textContent = "Updated.";
  });
</script>
Try It Yourself

How It Works

  1. Stable hooks. status and go mark the paragraph and button.
  2. Look up by id. getElementById("go") returns that one element.
  3. Update the target. On click, change the text of the status element.
  4. 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.

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 Chrome All
Full support
Mozilla Firefox All
Full support
Apple Safari All
Full support
Microsoft Edge All
Full support
Opera All
Full support
Samsung Internet All
Full support
Internet Explorer All
Full support
id Widely supported

Bottom line: Safe to use everywhere. Keep values unique and whitespace-free.

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.

Next: integrity

Verify fetched scripts and styles with cryptographic hashes.

integrity tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.

8 people found this page helpful