HTML id Global Attribute
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 p element with the id value my-paragraph:
const myParagraph = document.getElementById("my-paragraph");
// Do something with the paragraph 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>
Syntax
#id { ... }
Values
- idA name used to assign to the ID. Use only letters of the alphabet ( AZ, az ), numbers, hyphens, underscores.
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.
This global attribute is supported by all modern browsers.
Desktop
Tablets & Mobile
Last updated by CSSPortal on: 14th October 2023
