HTML id Attribute

Beginner
⏱️ 6 min read
📚 Updated: Jun 2026
🎯 3 Examples
Global & Structure

Introduction

In HTML, the id attribute is a fundamental global attribute that lets you uniquely identify an element within a document. Each element can have one id value, and that value must not be reused anywhere else on the same page. Developers rely on id for CSS targeting, JavaScript selection, in-page anchor links, and form label associations.

What You’ll Learn

01

Unique IDs

One per document.

02

CSS #id

Style one element.

03

Fragment Links

href="#footer"

04

getElementById

JavaScript hooks.

05

label for

Form accessibility.

06

id vs class

When to use each.

Purpose of id

The primary purpose of the id attribute is to serve as a unique identifier for an HTML element. This identifier is crucial for CSS rules that target a single element (#header), JavaScript that selects a specific node (document.getElementById("header")), hyperlinks that jump to a section (href="#footer"), and accessible label bindings (<label for="email">).

💡
Global attribute

The id attribute can be used on virtually any HTML element. It is not limited to one tag type—sections, headings, inputs, and links can all have an id when you need a unique hook.

📝 Syntax

Add id to any element with a unique string value:

id.html
<div id="header">
  <h1>Welcome to My Website</h1>
</div>

<p id="main-content">
  This is the main content of the page.
</p>

<a href="#footer">Jump to Footer</a>

<div id="footer">
  © 2026 My Website. All rights reserved.
</div>

Syntax Rules

  • Each id value must be unique within the entire HTML document.
  • Values must contain at least one character and must not contain ASCII spaces.
  • Use descriptive, readable names (e.g. main-nav, signup-form).
  • Start with a letter when possible so CSS selectors like #my-id work without escaping.
  • An element may have only one id attribute; it cannot list multiple ids like class.
  • Fragment links use href="#id-value" matching the target element’s id.

💎 Values

The id attribute accepts a string that uniquely names the element. Important considerations:

  • Uniqueness — Never reuse the same id on two elements. Duplicates break getElementById and fragment navigation.
  • Naming conventions — Use lowercase letters, digits, hyphens, underscores, colons, and periods. Avoid spaces and vague names like div1.
  • Case sensitivityid="Header" and id="header" are different values; stay consistent.
  • Reserved caution — Do not reuse ids that belong to browser extensions or third-party scripts on the same page.
id-values.html
<!-- Good: descriptive, unique -->
<nav id="main-nav">...</nav>
<section id="pricing">...</section>

<!-- Form label target -->
<label for="email">Email</label>
<input type="email" id="email" name="email">

⚡ Quick Reference

Use CaseExampleNotes
Assign unique idid="header"One id per element
CSS selector#header { ... }High specificity
JavaScriptgetElementById("header")Returns one element
Fragment linkhref="#footer"Scrolls to id="footer"
Label associationfor="email" + id="email"Accessibility
Global attributeMost HTML elementsNot duplicated in document

Applicable Elements

ElementSupported?Notes
<div>, <section>, <p>YesStructure and content regions
<a>, <button>YesInteractive element hooks
<input>, <textarea>, <select>YesPairs with <label for>
<h1><h6>YesSection heading targets
Global attributeYes (nearly all elements)Must remain unique per page

id vs class

AttributeUnique?Typical Use
idYes — one per documentSingle element hooks, fragment targets, JS entry points
classNo — reusableShared styling and component patterns
CSS selector#my-id.my-class
JavaScriptgetElementById()querySelectorAll(".class")

Examples Gallery

Page sections with fragment links, CSS and label targeting, and dynamic id changes with JavaScript.

👀 Live Preview

Jump to a section using a fragment link and unique id values:

Jump to footer

id="demo-id-header"

Example — Page Sections & Fragment Link

Let’s look at a simple example of how to use the id attribute in HTML:

id.html
<div id="header">
  <h1>Welcome to My Website</h1>
</div>

<p id="main-content">
  This is the main content of the page.
</p>

<a href="#footer">Jump to Footer</a>

<div id="footer">
  © 2026 My Website. All rights reserved.
</div>
Try It Yourself

How It Works

The id attribute is applied to the header, main-content, and footer elements. The anchor link uses href="#footer" to scroll to the element with id="footer".

Example — CSS Selector & Label Association

Target one element in CSS with #id and connect a label to a form control:

id-css-label.html
<style>
  #hero-title { color: #1d4ed8; }
</style>

<h1 id="hero-title">Welcome</h1>

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

How It Works

The CSS rule #hero-title styles exactly one heading. The label’s for="username" must match the input’s id="username" so assistive technologies and clicks activate the correct field.

Dynamic Values with JavaScript

You can dynamically set the id attribute using JavaScript when modifying elements at runtime:

dynamic-id.html
<div id="dynamic-element">Original id</div>

<script>
  document.getElementById("dynamic-element").id = "new-dynamic-id";
</script>
Try It Yourself

How It Works

Assigning a new string to element.id updates the attribute immediately. Any code still calling getElementById("dynamic-element") will fail unless you update references to the new id.

♿ Accessibility

  • Pair labels with ids — Use <label for="field-id"> matching id on inputs so screen readers announce the correct label.
  • Provide skip links — An id on <main> (e.g. id="main-content") lets skip-navigation links jump past repetitive headers.
  • Support ARIA references — Attributes like aria-labelledby and aria-describedby point to other elements by their id.
  • Do not rely on id alone for meaning — Use semantic elements (<nav>, <main>, headings) in addition to ids.
  • Keep ids stable — Changing ids breaks bookmarks, fragment links, and assistive technology references.

🧠 How id Works

1

Author assigns unique id

One distinctive name per element.

Markup
2

Browser indexes the id

Available to CSS, links, and JS.

Parse
3

Tools reference the id

#selector, #fragment, getElementById.

Use
=

Precise element targeting

Style, script, link, and label one specific node reliably.

Browser Support

The id attribute is supported in all browsers on virtually every HTML element. Fragment navigation, CSS #id selectors, and document.getElementById() behave consistently across Chrome, Firefox, Safari, and Edge.

HTML · Fully supported

Universal id support

All major browsers honor unique id values for styling, scripting, and in-page navigation.

99% Browser support
Google Chrome Fully supported
Full support
Mozilla Firefox Fully supported
Full support
Apple Safari Fully supported
Full support
Microsoft Edge Fully supported
Full support
Internet Explorer Fully supported
Full support
Opera Fully supported
Full support
id attribute 99% supported

Bottom line: Use id confidently for unique element hooks across all modern browsers.

💡 Best Practices

✅ Do

  • Ensure every id is unique within the document
  • Use descriptive names that reflect purpose or content
  • Use id for fragment targets and JavaScript entry points
  • Pair form control ids with matching <label for> values
  • Prefer class for styling shared across many elements

❌ Don’t

  • Reuse the same id on multiple elements
  • Use generic ids like div1 or box
  • Overuse ids for every element when a class would suffice
  • Include spaces or special characters that break CSS selectors
  • Change ids casually without updating links and scripts

Conclusion

The id attribute is a crucial aspect of HTML for uniquely identifying and referencing elements within a document. It connects markup to CSS, JavaScript, navigation, and accessible form labels.

By following best practices—unique values, meaningful names, and sparing use where class is enough—you can enhance the structure, styling, and interactivity of your web pages.

Key Takeaways

Knowledge Unlocked

Five truths every developer should know about id

Bookmark these before adding ids to your next page.

5
Core concepts
🎨 02

# CSS

Target one element

Styling
⚙️ 03

getElementById

JavaScript hook

Scripting
🔗 04

#fragment

In-page jumps

Links
05

label for

Form a11y

Accessibility

❓ Frequently Asked Questions

It assigns a unique identifier to an HTML element. That id can be targeted by CSS (#id), JavaScript (getElementById), fragment links (href="#id"), and label associations (for="id").
No. Duplicate ids are invalid HTML and cause unpredictable behavior in CSS, JavaScript, and accessibility tools. Always keep ids unique per document.
id identifies one specific element. class can be applied to many elements for shared styling or behavior. Use id for unique hooks; use class for reusable patterns.
id is a global attribute supported on almost all HTML elements, including structural tags, text, links, and form controls.
An anchor with href="#footer" scrolls to the element that has id="footer". The hash value must exactly match the target element’s id.
Yes. Set element.id = "newId" or use setAttribute("id", "newId"). Update any CSS, links, or scripts that still reference the old id.

Build well-structured pages

Practice the id attribute with page sections, CSS selectors, and JavaScript in the Try It editor.

Try page sections example →

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.

5 people found this page helpful