The id Attribute
The id Attribute
Section titled “The id Attribute”The id attribute provides a unique identifier for an HTML element within a document. Each id value must be unique across the entire page, making it perfect for targeting specific elements with CSS, JavaScript, or fragment identifiers.
Syntax
Section titled “Syntax”<element id="unique-identifier">Content</element>Values
Section titled “Values”| Value | Description |
|---|---|
text | A unique string that identifies the element. Must contain at least one character and cannot contain spaces. Case-sensitive. |
Examples
Section titled “Examples”Basic Usage
Section titled “Basic Usage”Fragment Identifiers (Page Anchors)
Section titled “Fragment Identifiers (Page Anchors)”JavaScript Targeting
Section titled “JavaScript Targeting”Accessibility with Labels
Section titled “Accessibility with Labels”Best Practices
Section titled “Best Practices”Use Meaningful IDs
Section titled “Use Meaningful IDs”Choose descriptive names that reflect the element’s purpose:
<!-- Good --><nav id="main-navigation"></nav><form id="contact-form"></form><div id="search-results"></div>
<!-- Avoid --><nav id="nav1"></nav><form id="form"></form><div id="div3"></div>Follow Naming Conventions
Section titled “Follow Naming Conventions”Use consistent naming patterns throughout your project:
<!-- kebab-case (recommended) --><header id="site-header"></header><div id="user-profile-card"></div>
<!-- camelCase (also common) --><header id="siteHeader"></header><div id="userProfileCard"></div>Avoid Starting with Numbers
Section titled “Avoid Starting with Numbers”While modern browsers support it, IDs starting with numbers can cause issues:
<!-- Problematic in CSS --><div id="1st-section"></div>
<!-- Better --><div id="first-section"></div><div id="section-1"></div>Keep IDs Unique
Section titled “Keep IDs Unique”Never reuse the same ID on multiple elements:
<!-- Wrong - duplicate IDs --><div id="content">First</div><div id="content">Second</div>
<!-- Correct - unique IDs --><div id="content-primary">First</div><div id="content-secondary">Second</div>Accessibility Considerations
Section titled “Accessibility Considerations”Form Labels
Section titled “Form Labels”The id attribute enables proper form labeling, crucial for screen readers:
<label for="username">Username:</label><input type="text" id="username" name="username">When a user clicks the label, the associated input receives focus, improving usability.
ARIA Relationships
Section titled “ARIA Relationships”IDs are essential for ARIA attributes that establish relationships between elements:
<button aria-describedby="help-text">Submit</button><div id="help-text">Click to submit the form</div>
<h2 id="dialog-title">Confirm Action</h2><div role="dialog" aria-labelledby="dialog-title"> <p>Are you sure you want to continue?</p></div>Skip Links
Section titled “Skip Links”Use IDs to create skip navigation links for keyboard and screen reader users:
<a href="#main-content" class="skip-link">Skip to main content</a><!-- Other navigation --><main id="main-content"> <!-- Page content --></main>Browser Support
Section titled “Browser Support”The id attribute is supported by all browsers and has been part of HTML since the beginning.
| Browser | Support |
|---|---|
| Chrome | All versions |
| Firefox | All versions |
| Safari | All versions |
| Edge | All versions |
Common Use Cases
Section titled “Common Use Cases”CSS Styling
Section titled “CSS Styling”<div id="hero-banner"> <h1>Welcome!</h1></div>
<style> #hero-banner { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); padding: 60px 20px; text-align: center; }</style>JavaScript Manipulation
Section titled “JavaScript Manipulation”<div id="counter">0</div><button onclick="increment()">Increment</button>
<script> function increment() { const counter = document.getElementById('counter'); counter.textContent = parseInt(counter.textContent) + 1; }</script>URL Fragments
Section titled “URL Fragments”<!-- In URL: https://example.com/page.html#contact --><section id="contact"> <h2>Contact Us</h2> <!-- Form content --></section>Related Attributes
Section titled “Related Attributes”class- Apply styles to multiple elementsname- Identify form elements (can be duplicated)data-*- Store custom data on elements