HTML id attribute provides a unique identifier for an element within a document. It allows targeted selection and manipulation of the element through CSS and JavaScript, facilitating specific styling and functionality. In CSS, the id attribute is used using the # symbol followed by id. quotes are not mandatory in tag=” ” in all cases. But writing with quotes is a good practice.
HTML Id Attribute Syntax:
<tag id=""></tag>
Note: This is a global attribute, it can be used in all the tags.
Using The id Attribute Example:
In this example, we simply style the element with id “geeks”.
HTML
<!DOCTYPE html>
<html>
<head>
<style>
#geeks {
color: green;
}
</style>
</head>
<body>
<h2>Welcome to GeeksforGeeks</h2>
<h1 id="geeks">Hi Geeks!</h1>
</body>
</html>
Output:

HTML id Attribute
Explanation:
- In this example we includes two headings one displaying “Welcome to GeeksforGeeks” and the other with the id “geeks”.
- CSS targets the element with id “geeks”, setting its text color to green, creating a visually distinct appearance.
- By styling the second heading uniqHTML Id AttributeHTML Id Attributeuely, it stands out from the rest of the content, drawing attention to the specific message “Hi Geeks!”.
Using The id Attribute Example:
In this example, we are adding the styling properties to the specific id attribute value by fetching its id value.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Id Attributes</title>
<style>
#gfg {
color: #009900;
font-size: 50px;
font-weight: bold;
text-align: center;
}
#geeks {
text-align: center;
font-size: 20px;
}
</style>
</head>
<body>
<div id="gfg">GeeksforGeeks</div>
<div id="geeks">
A computer science portal for geeks
</div>
</body>
</html>
Output:

Adding the style properties to the specific id attribute value
Explanation
- Here The HTML document contains two <div> elements with unique id attributes: “gfg” and “geeks”.HTML Id Attribute
- CSS styles are applied to each id selector to modify properties such as text color, font size, font weight, and text alignment.
- The “gfg” id styles affect the “GeeksforGeeks” text, making it large, bold, and centered. The “geeks” id styles modify the description text, adjusting its alignment and size.
Note: In HTML5, id attributes can be used by any HTML tag but in HTML 4.01 there are some restriction to use id attributes. It can not be used by <base>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title> tag. In HTML4.01 id can not start with number.
Use of ID attributes in JavaScript:
In JavaScript, ID attributes uniquely identify HTML elements, enabling efficient selection and manipulation through methods like getElementById(). They serve as hooks for accessing specific elements for dynamic functionality and interaction.
ID attributes in JavaScript Example :
This example describes getting the id attribute value in Javascript through getElementById() Method.
HTML
<!DOCTYPE html>
<html>
<head>
<title>Using the id in Javascript</title>
<style>
#geeks {
font-size: 50px;
color: #009900;
font-weight: bold;
margin-bottom: 10px;
}
</style>
</head>
<body>
<div id="geeks">GeeksforGeeks</div>
<button onclick="geeksResult()">Display text change</button>
<script>
function geeksResult() {
document.getElementById("geeks").innerHTML =
"A computer science portal for geeks";
document.getElementById("geeks").style.color = "black";
}
</script>
</body>
</html>
Output:

Getting the id attribute value using getElementById() Method
Explanation:
- In the above example Includes a div element with id “geeks”, displaying “GeeksforGeeks” initially.
- Clicking the button triggers a JavaScript function called “geeksResult()”.
- The function modifies the innerHTML of the div element to “A computer science portal for geeks”.
HTML Id Attribute Use Cases
The id attribute in HTML uniquely identifies elements, enabling targeted selection and manipulation via CSS and JavaScript for specific functionality and styling.
The id attribute uniquely identifies one element, while the class attribute can be applied to multiple elements for shared styling.
Here Valid values for the id attribute in HTML are unique identifiers consisting of letters, numbers, hyphens, underscores, and colons, starting with a letter or underscore.
To select an element by ID in JavaScript, use the getElementById() method followed by the ID name enclosed in quotes.
HTML Id Attribute Supported Browsers:
Similar Reads
HTML accept Attribute
HTML accept Attribute specifies the type of file that the server accepts. This attribute can be used with <input> element only. This attribute is not used for validation tools because file uploads should be validated on the Server. Syntax: <input accept = "file_extension"> Note: This att
3 min read
HTML accept-charset Attribute
The accept-charset attribute is used to define the character encoding and is used for form submission. The default value of the accept-charset attribute is "UNKNOWN" string which indicates the encoding equals to the encoding of the document containing the <form> element. Syntax: <form accep
2 min read
HTML accesskey Attribute
The HTML accesskey attribute defines a keyboard shortcut to activate or focus an element. It assigns a keyboard key combination to trigger an element (like a link or button).Browser support and specific key combinations vary (e.g., Alt + key on Windows, Ctrl + key on macOS).Use it sparingly and prov
3 min read
HTML action Attribute
The HTML action attribute is used to specify where the form data should be sent on submission. It allows the browser to send the data to the specified location, enabling server-side scripts to process the data and generate a response. Note: It can be used in the <form> element. Syntax:<form
3 min read
HTML align Attribute
In HTML, the align attribute is used to control the alignment of elements on a webpage. Whether it's for text, images, or tables, the align attribute helps to position content in relation to its surrounding elements. Syntax <element_name align="left | right | center | justify">Attribute Values
4 min read
HTML alt attribute
The alt attribute in HTML provides alternative text for images, aiding accessibility and providing context for screen readers. Syntax: <img src=" " alt=" " >[GFGTABS] HTML <html> <body> <h1>GeeksforGeeks Logo</h1> <img src="https://media.geeksforgeeks.org/wp-con
3 min read
HTML async Attribute
The HTML <script> async attribute is used to load and execute external scripts without blocking the rest of the page from loading. This speeds up page load times. It only works with external scripts (when the src attribute is present). Syntax:<script src="path-to-script.js" async></sc
3 min read
HTML autocomplete Attribute
The HTML autocomplete Attribute is used to specify whether the input field autocompleted would be on or off. When the autocomplete attribute is set to on the browser will automatically complete the values based on what the user entered before. It works with input fields such as text, search, URL, em
3 min read
HTML autoplay Attribute
The HTML autoplay Attribute, a boolean attribute, enables audio or video elements to start playing automatically when the page loads, providing seamless playback without interruption. Syntax: <element autoplay> Supported ElementsIt can be used with <audio> and <video> elements. El
2 min read
HTML autofocus Attribute
The HTML autofocus attribute is a powerful tool that enhances user experience by automatically setting the focus to a specific element when a webpage loads. Syntax<input type="text" autofocus> Note: This attribute is boolean, meaning it can either be present or absent. Supported TagsElementPur
2 min read