Definition and Usage
The id attribute specifies a unique identifier (ID) for an element.
The value of this attribute must be unique and distinct within the document (web page).
Basic Example
<a href="#section2">Go to Section 2</a>
<!-- Click the link above to scroll down to this element -->
<div id="section2">
<h2>Section 2 Content</h2>
</div>
Practical Uses
The id attribute is a global attribute.
The unique identifier assigned to an element through this attribute can be used for various purposes.
Using a Unique CSS Selector to Style an Element
The following example uses the value specified in the id attribute as a selector to style the element.
#css-selector { /* Select by id attribute value: #id-value selector */
background-color: yellowgreen;
}
<p>Paragraph element.</p>
<p id="css-selector">Paragraph element.</p>
<p>Paragraph element.</p>
<p>Paragraph element.</p>
CSS can apply styles by selecting HTML elements in various ways.
Among these methods, you can also select an element using the value specified in the id attribute. As shown in the example above, enter the ID value after the hash symbol (#) (#css-selector).
This allows you to apply styles specifically to the element whose ID value is css-selector. In the actual result, the background color of the element is styled with yellowgreen.
Paragraph element.
Paragraph element.
Paragraph element.
Paragraph element.
Identifying an Element with JavaScript
The following example uses the value specified in the id attribute to identify an element in JavaScript.
<p id="txt">Welcome.</p>
<button type="button" id="btn">Click to change the text</button>
<script>
const txt = document.getElementById("txt"); // Identify id="txt"
const btn = document.getElementById("btn"); // Identify id="btn"
btn.addEventListener("click", () => {
txt.innerHTML = "Nice to meet you.";
});
</script>
In the code example above, clicking the <button> element with id="btn" changes the text of the element with id="txt" from "Welcome." to "Nice to meet you.".
At this point, when JavaScript identifies the <button> element with id="btn" or the element with id="txt", both elements are identified using the values specified in their id attributes.
Welcome.
Identifying an Element with a URL Fragment
First, let's look at what a fragment in a URL (or URI) is.
A URL consists of parts such as a domain and the path commonly used to access a web page. Here is an example of a URL that includes a hash symbol (#) at the end of a typical URL.
Here, it refers to the #example portion highlighted in red. This is called a fragment or a hash URL.
In a URL, the fragment is the string that follows the hash symbol (#), and it corresponds to the value specified in the id attribute within the web page of that URL. When a web page with a fragment in its URL is loaded, the browser finds the element whose id attribute value matches the fragment and scrolls the page so that the element appears at the top of the visible area of the screen (called the viewport).
<a href="#section2">Go to Section 2</a>
<!-- Click the link above to scroll down to this element -->
<div id="section2">
<h2>Section 2 Content</h2>
</div>
In this way, the value specified in the id attribute is also used to identify the element as a URL fragment identifier.
Using a Unique Identifier to Associate an Element with Another Element
In HTML, some attributes connect elements by referencing another element’s id value, thereby establishing a relationship between them.
At this point, the id value is used to uniquely identify the element being referenced.
This allows you to define relationships between elements or implement specific related features.
Let’s look at the following example.
Example: Connecting a <label> to an <input>
The <label> represents a text label (or caption) for user interface control elements. It improves readability and web accessibility by providing a name for the control element.
When connecting a <label> to a form control element, the value of the target element’s id attribute is assigned to the for attribute.
When this is set, the two elements are explicitly connected, and browsers and assistive technologies such as screen readers use this relationship to improve user experience and accessibility.
<!--
The id value "user-checking" of the target form control element
is assigned to the label's for attribute.
-->
<label for="user-checking">Click me.</label>
<input type="checkbox" id="user-checking">
<label> element ("Click me.") moves focus to the associated checkbox.
Naming Rules for the id Attribute Value
- The value of this attribute must be unique and distinct within the document (web page).
- The
idattribute value must contain one or more characters with no whitespace characters (such as spaces or tabs). - It can consist entirely of numbers, start with a number, start with an underscore (
_), or consist solely of punctuation marks (.). There are no other specific restrictions. - It is case-sensitive.
Things to Keep in Mind
It must be unique and distinct within the document (web page).
If there are duplicate id attribute values, unexpected errors may occur across all CSS, JavaScript, and other applications built on the assumption that the id attribute value is a unique identifier, such as when using it as a unique CSS selector to style the element, identifying it via JavaScript, or targeting it as a URL fragment identifier.
<p>Paragraph element.</p>
<p id="css-selector">Paragraph element.</p>
<p id="css-selector">Paragraph element.</p> <!-- Duplicate -->
<p>Paragraph element.</p>
Browser Compatibility
| Attribute |
Desktop Chrome
|
Desktop Edge
|
Desktop Firefox
|
Safari
|
|---|---|---|---|---|
id
|
1 | 12 | 32 | 1 |
Specifications
| Specification | |
|---|---|
id
|
HTML Standard #global-attributes:the-id-attribute-2 |
References
See Also
- HTML class Attribute – Proper Understanding and Usage
- HTML title Attribute – Proper Understanding and Usage
- HTML cite Attribute – Referencing the Source URL
- HTML datetime Attribute – Specifying the Date and Time
- HTML rel Attribute – Meaning, List, and Examples
- HTML contenteditable Attribute – Making HTML Elements Editable
- HTML enterkeyhint Attribute – Specifying the Enter key action for virtual keyboards
- HTML inputmode Attribute – Specifying the Virtual Keyboard Input Mode
- HTML data-* Attributes – Standard Custom Data Attributes
- HTML hidden Attribute – Proper Understanding and Usage