Skip to content

The id Attribute

Global 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.

<element id="unique-identifier">Content</element>
ValueDescription
textA unique string that identifies the element. Must contain at least one character and cannot contain spaces. Case-sensitive.
Result
Result
Result
Result

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>

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>

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>

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>

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.

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>

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>

The id attribute is supported by all browsers and has been part of HTML since the beginning.

BrowserSupport
ChromeAll versions
FirefoxAll versions
SafariAll versions
EdgeAll versions
<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>
<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>
<!-- In URL: https://example.com/page.html#contact -->
<section id="contact">
<h2>Contact Us</h2>
<!-- Form content -->
</section>
  • class - Apply styles to multiple elements
  • name - Identify form elements (can be duplicated)
  • data-* - Store custom data on elements