👀 Live Preview
Jump to a section using a fragment link and unique id values:
id="demo-id-header"
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.
One per document.
Style one element.
href="#footer"
JavaScript hooks.
Form accessibility.
When to use each.
idThe 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">).
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.
Add id to any element with a unique string value:
<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>id value must be unique within the entire HTML document.main-nav, signup-form).#my-id work without escaping.id attribute; it cannot list multiple ids like class.href="#id-value" matching the target element’s id.The id attribute accepts a string that uniquely names the element. Important considerations:
id on two elements. Duplicates break getElementById and fragment navigation.div1.id="Header" and id="header" are different values; stay consistent.<!-- 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">| Use Case | Example | Notes |
|---|---|---|
| Assign unique id | id="header" | One id per element |
| CSS selector | #header { ... } | High specificity |
| JavaScript | getElementById("header") | Returns one element |
| Fragment link | href="#footer" | Scrolls to id="footer" |
| Label association | for="email" + id="email" | Accessibility |
| Global attribute | Most HTML elements | Not duplicated in document |
| Element | Supported? | Notes |
|---|---|---|
<div>, <section>, <p> | Yes | Structure and content regions |
<a>, <button> | Yes | Interactive element hooks |
<input>, <textarea>, <select> | Yes | Pairs with <label for> |
<h1> – <h6> | Yes | Section heading targets |
| Global attribute | Yes (nearly all elements) | Must remain unique per page |
id vs class| Attribute | Unique? | Typical Use |
|---|---|---|
id | Yes — one per document | Single element hooks, fragment targets, JS entry points |
class | No — reusable | Shared styling and component patterns |
| CSS selector | #my-id | .my-class |
| JavaScript | getElementById() | querySelectorAll(".class") |
Page sections with fragment links, CSS and label targeting, and dynamic id changes with JavaScript.
Jump to a section using a fragment link and unique id values:
id="demo-id-header"Let’s look at a simple example of how to use the id attribute in 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>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".
Target one element in CSS with #id and connect a label to a form control:
<style>
#hero-title { color: #1d4ed8; }
</style>
<h1 id="hero-title">Welcome</h1>
<label for="username">Username</label>
<input type="text" id="username" name="username">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.
You can dynamically set the id attribute using JavaScript when modifying elements at runtime:
<div id="dynamic-element">Original id</div>
<script>
document.getElementById("dynamic-element").id = "new-dynamic-id";
</script>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.
<label for="field-id"> matching id on inputs so screen readers announce the correct label.id on <main> (e.g. id="main-content") lets skip-navigation links jump past repetitive headers.aria-labelledby and aria-describedby point to other elements by their id.<nav>, <main>, headings) in addition to ids.One distinctive name per element.
Available to CSS, links, and JS.
#selector, #fragment, getElementById.
Style, script, link, and label one specific node reliably.
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.
All major browsers honor unique id values for styling, scripting, and in-page navigation.
Bottom line: Use id confidently for unique element hooks across all modern browsers.
id is unique within the documentid for fragment targets and JavaScript entry points<label for> valuesclass for styling shared across many elementsid on multiple elementsdiv1 or boxThe 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.
idBookmark these before adding ids to your next page.
One id per page.
RuleTarget one element
StylingJavaScript hook
ScriptingIn-page jumps
LinksForm a11y
Accessibility#id), JavaScript (getElementById), fragment links (href="#id"), and label associations (for="id").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.href="#footer" scrolls to the element that has id="footer". The hash value must exactly match the target element’s id.element.id = "newId" or use setAttribute("id", "newId"). Update any CSS, links, or scripts that still reference the old id.Practice the id attribute with page sections, CSS selectors, and JavaScript in the Try It editor.
5 people found this page helpful