HTML id Global Attribute
The id attribute gives an element a unique identifier, used to style it with CSS, find it with JavaScript, link to it with a URL fragment and connect it to labels.
Description
The id HTML global attribute specifies a unique identifier for an HTML element. It can be used on any HTML element, and its value must be unique within the document. The id attribute is used for a variety of purposes, including:
- Styling: CSS selectors can be used to target elements with specific
idvalues. This allows you to apply unique styles to individual elements. - Scripting: JavaScript can be used to access and manipulate elements with specific
idvalues. This allows you to dynamically control the behavior of your web pages. - Linking: You can use fragment identifiers to link directly to specific elements on a page. This is useful for creating navigation within a long page or for sharing specific content with others.
Here is an example of how to use the id attribute:
<h1 id="main-heading">This is the main heading</h1>
<p>This is some text.</p>
This CSS selector would target the h1 element with the id value main-heading:
#main-heading {
color: red;
}
This JavaScript code would access the h1 element with the id value main-heading:
const heading = document.getElementById("main-heading");
// Do something with the heading element
This fragment identifier would link directly to the h1 element with the id value main-heading:
<a href="#main-heading">Go to the main heading</a>
The id is also used to connect elements, for example a <label for> to its field, aria-labelledby, popovertarget and commandfor.
Syntax
<element id="id">
Values
- idA unique identifier. It must contain at least one character and no whitespace, and is case-sensitive. Using letters, digits, hyphens and underscores, and starting with a letter, makes it easiest to use in CSS selectors and URLs.
Example
Browser Support
The following information will show you the current browser support for the HTML id global attribute. Hover over a browser icon to see the version that first introduced support for this HTML global attribute.
Desktop





Tablets & Mobile





