The Wayback Machine - https://web.archive.org/web/20240903141156/https://www.geeksforgeeks.org/html-id-attribute/
Open In App

HTML Id Attribute

Last Updated : 29 Aug, 2024
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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:

id attributes

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

1. What is the use of id attribute in HTML ?

The id attribute in HTML uniquely identifies elements, enabling targeted selection and manipulation via CSS and JavaScript for specific functionality and styling.

2.Difference between id and class Attributes in HTML

The id attribute uniquely identifies one element, while the class attribute can be applied to multiple elements for shared styling.

3.What are valid values for the id attribute in HTML?

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.

4.How to Select an Element by ID in JavaScript ?

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:

HTML Id Attribute – FAQs

Can multiple elements have the same id in HTML?

No, each id value must be unique across a single HTML document. Using the same id for multiple elements is invalid HTML and may cause issues with CSS and JavaScript.

How is the id attribute different from the class attribute?

The id attribute is unique and should be used for a single element, while the class attribute can be shared by multiple elements. id is primarily used for one specific element, while class is used to style multiple elements.

How do you reference an element by id in CSS?

Use the # symbol followed by the id value in your CSS. Example: #header { color: blue; } targets an element with id=”header”.

How do you reference an element by id in JavaScript?

Use document.getElementById(“idValue”) to access an element by its id. Example: let header = document.getElementById(“header”); targets the element with id=”header”.

Can the id attribute be used for internal page linking?

Yes, you can link to a specific section of a page using the id attribute. Example: <a href=”#section1″>Go to Section 1</a> links to an element with id=”section1″.

Is the id attribute case-sensitive?

Yes, the id attribute is case-sensitive in HTML. id=”Header” and id=”header” are considered different identifiers.



Previous Article
Next Article

Similar Reads

How href attribute is different from src attribute in HTML ?
In HTML5, the href and src attributes play distinct roles in defining paths or URLs, each associated with specific HTML elements. The href attribute, commonly found in an anchor (&lt;a&gt;) elements, points to the destination of hyperlinks, facilitating navigation. The src attribute, used with elements like &lt;img&gt; and &lt;script&gt;, specifies
1 min read
When to use the class attribute and the id attribute ?
The class attribute is used when multiple HTML elements share a common style or behaviour, allowing the application of a shared style to several elements. It promotes code reusability and is suitable for elements with similar characteristics. The id attribute is utilized when a unique identifier is needed for a specific HTML element. This identifie
1 min read
How to use the target attribute and the rel attribute in the &lt;a&gt; Tag ?
The target and rel attributes in the &lt;a&gt; (anchor) tag is used to control the behaviour of hyperlinks, specifying how the linked content should be opened and establishing relationships between the current and linked documents. Here's how to use these attributes: target Attribute:The target attribute determines where the linked content will be
2 min read
HTML | &lt;html&gt; xmlns Attribute
The HTML &lt;html&gt; xmlns Attribute is used to specify the xml namespace for a document. Important Note: This attribute is mainly required in XHTML, not valid in HTML 4.01, and optional in HTML 5. Syntax: &lt;html xmlns="http://www.w3.org/1999/xhtml"&gt; Attribute Values: https://www.geeksforgeeks.org/ It defines the namespace to use (for XHTML d
1 min read
HTML | scoped Attribute
The HTML scoped attribute is a boolean attribute that is used to specify that the styles only apply to this element's parent element and that element's child elements (not the entire document).Note : It is a deprecated attribute and it is not supported by HTML5. Supported tag: style Example: C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head
1 min read
HTML | &lt;th&gt; valign Attribute
The HTML &lt;th&gt; valign Attribute is used to specify the vertical alignment of text content in a header cell. It is not supported by HTML 5. Syntax: &lt;th valign="top | middle | bottom | baseline"&gt; Attribute Value: top: It sets the table header content to top-align. middle: It sets the table header content to middle-align. bottom: It sets th
1 min read
HTML | &lt;col&gt; align Attribute
The HTML &lt;col&gt; align Attribute is used to set the horizontal alignment of text content inside the col element. It is not supported by HTML 5. Syntax: &lt;col align="left | right | center | justify | char"&gt; Attribute Values: left: It sets the text left-align. right: It sets the text right-align. center: It sets the text center-align. justif
1 min read
HTML | poster Attribute
The HTML poster Attribute is used to display the image while video downloading or when the user clicks the play button. If this image not set then it will take the first frame of video as a poster image.Applicable: It is mainly used in &lt;video&gt; element. Syntax: &lt;video poster="URL"&gt; Attribute Values: It contains a single value URL which s
1 min read
HTML | &lt;area&gt; target Attribute
The HTML &lt;area&gt; target Attribute is used to specify where to open the linked document. It works only when href attribute is present in the area elements. Syntax: &lt;area target="_blank|_self|_parent|_top|framename"&gt; Attribute Values: _blank: It opens the link in a new window._self: It is the default value. It opens the linked document in
1 min read
HTML | &lt;select&gt; autocomplete Attribute
The HTML &lt;select&gt; autocomplete attribute is used to specify that the form has autocompleted on or off. When the autocomplete attribute is set to on the browser will automatically complete the values based on which the user entered before. Syntax: &lt;select autocomplete="on | off"&gt; Attribute Values: on: It is the default value. When the au
1 min read
HTML &amp; CSS | Tabindex attribute &amp; Navigation bars
The tabindex attribute specifies the tab order of an element. "tab" button is used for navigation. The tabindex content attribute allows users to control whether an element is supposed to be focusable, whether it is supposed to be reachable using sequential focus navigation, and what is to be the relative order of the element for the purposes of se
2 min read
HTML | &lt;table&gt; bgcolor Attribute
The HTML &lt;table&gt; bgcolor Attribute is use to specify the background color of a table.Syntax: &lt;table bgcolor="color_name | hex_number | rgb_number"&gt; Attribute Values: color_name: It sets the text color by using the color name. For example "red".hex_number: It sets the text color by using the color hex code. For example "#0000ff".rgb_numb
1 min read
HTML | srcdoc Attribute
The HTML srcdoc attribute is used to specify the HTML content of the page to show in the inline frame. This attribute is anticipated to be used beside the sandbox and seamless attributes. If a browser supports the srcdoc attribute, it'll override the content laid out in the src attribute (if present). If a browser doesn't support the srcdoc attribu
1 min read
HTML &lt;video&gt; crossorigin Attribute
The HTML crossorigin attribute is used in the &lt;video&gt; element that supports a CORS request when we tried to fetch out the video files from the third-party servers or from another domain. Syntax: &lt;video crossorigin="anonymous | use-credentials"&gt; Attribute Values: anonymous: It has a default value. It defines a CORS request that will be s
1 min read
HTML | Marquee truespeed attribute
If the scrolldelay value less then 60 and truespeed mode is on then the value of scrolldelay is not effecting the scrolling. If the scrolldelay value more then 60 and truespeed mode is on then the truespeed is not effecting the scrolling. So, basically it is used to set scrollDelay attribute should be taken as an exact value or not.Syntax: &lt;marq
1 min read
What is the significance of adding autocomplete attribute in HTML Form ?
The HTML &lt;form&gt; autocomplete attribute is used to specify that the form has autocompleted on or off value. When the autocomplete attribute is set to "on", the browser will automatically complete the values based on which the user entered before. Syntax: &lt;form autocomplete="on|off"&gt; Attribute values: on: It has a default value. When the
1 min read
HTML | &lt;td&gt; abbr Attribute
The HTML &lt;td&gt; abbr attribute is used to specify the shorter version of the content. This attribute is not supported by HTML 5. It does not describe any visual effect but it is used by screen readers. Syntax: &lt;td abbr="text"&gt; Attribute Values: text: It contains the short description of cell content. Note : This attribute is not supported
1 min read
HTML reversed Attribute
It is a Boolean Attribute and is used to order the list in Descending Order(9, 8, 7, 6 .....) instead of ascending order(1, 2, 3 ....). Syntax: &lt;ol reversed&gt; &lt;li&gt; Content... &lt;/li&gt; &lt;li&gt; Content... &lt;/li&gt; ... &lt;li&gt; Content... &lt;/li&gt;&lt;/ol&gt;Applicable: &lt;ol&gt; Attributes: It includes single attributes rever
1 min read
HTML onsearch Event Attribute
This onsearch attribute works when a user presses the ENTER button or clicks on the x button. This event works with &lt;input&gt; element. Supported Tags: &lt;input type="search"&gt;Syntax: &lt;element onsearch="script"&gt;Attribute: This event contains single attribute script which contains the script to be run when event call. Example: C/C++ Code
1 min read
HTML oninput Event Attribute
This attribute works when it gets user input value. This attribute mainly fires when the user change the value of &lt;input&gt; and &lt;textarea&gt; element. This attribute is quite similar to onchange attribute but the basic difference is that the oninput event attribute occurs immediately when the value of the element changes while the onchange a
1 min read
HTML | onmouseup Event Attribute
This attribute fires when a mouse button is released over the element. The order of events occur related to the onmouseup event. onmousedownonmouseuponclick Supported Tags: All HTML elements, EXCEPT: &lt;base&gt; &lt;bdo&gt;&lt;br&gt;&lt;head&gt;&lt;html&gt;&lt;iframe&gt;&lt;meta&gt;&lt;param&gt;&lt;script&gt;&lt;style&gt; &lt;title&gt; Syntax:
1 min read
HTML | onwheel Event Attribute
This attribute works when the wheel of pointer device is rolled up or down over an element. The onwheel attribute also works when the user scrolls or zooms on an element by using mouse or touchpad. Supported Tags: It supports all HTML elements. Syntax: &lt;element onwheel = "script"&gt; Attribute Value: This attribute contains single value script a
1 min read
HTML | oncontextmenu Event Attribute
This attribute works when the user right-clicks on an element to open the context menu.Supported Tags: It supports all HTML elements. Syntax: &lt;element oncontextmenu="script"&gt; Attribute Value: This attribute contains single value script. The script to be run when oncontextmenu attribute called. It is supported by all HTML elements.Note: This a
1 min read
HTML | onfocus Event Attribute
This onfocus attribute works when the element gets focused. This event attribute is mostly used with &lt;input&gt;, &lt;select&gt;, &lt;a&gt; elements. This event attribute is supported by all HTML elements excepts &lt;base&gt;, &lt;bdo&gt;, &lt;br&gt;, &lt;head&gt;, &lt;html&gt;, &lt;iframe&gt;, &lt;meta&gt;, &lt;param&gt;, &lt;script&gt;, &lt;sty
1 min read
HTML | &lt;iframe&gt; width Attribute
The HTML &lt;iframe&gt; width attribute is used to specify the width of the &lt;iframe&gt; in terms of pixels. Syntax: &lt;iframe width="pixels"&gt; Attribute Values: It contains single value pixel which specifies the width of the iframes in terms of pixels. Below example illustrates the use of &lt;iframe&gt; width attribute in HTML: Example: C/C++
1 min read
HTML &lt;a&gt; ping Attribute
The HTML &lt;a&gt; ping Attribute is generally used to either specify a particular URL or a list of URLs that will be notified when the user will click on the hyperlink passed int HTML &lt;a&gt; href Attribute. This is achieved by sending a short HTTP post request to the specified URL as soon as the user clicks on the hyperlink. Syntax : &lt;a href
1 min read
HTML | &lt;input&gt; formtarget Attribute
The HTML &lt;input&gt; formtarget attribute is used to specify the name or a keyword which indicates where to display the response after submitting the form. This attribute overrides the target attribute of &lt;form&gt; element. Syntax: &lt;input formtarget="_blank|_self|_parent|_top|framename"&gt; Attribute Values: _blank: The input response is di
1 min read
HTML | ononline Event Attribute
The ononline event attribute works when the browser starts working in online mode. it is opposite to onoffline event attribute.Supported Tags &lt;body&gt; syntax &lt;element ononline = "script"&gt; Attribute Value: This event attribute contains single value script which works when browser working in online mode.Example: C/C++ Code &lt;!DOCTYPE HTML
1 min read
HTML | &lt;object&gt; border Attribute
The HTML &lt;object&gt; border attribute is used to define the width of the border around the object element. Note: This attribute is not supported by HTML5. Syntax: &lt;object border="pixels"&gt; Attribute Values: pixels: It is used to specify the width of the border in terms of pixel. Example: This example illustrates the use of &lt;object&gt; bo
1 min read
HTML open Attribute
The open attribute in HTML is used to indicate whether the details will be shown on page load. This is a boolean attribute. If it is not present by default then details are not shown. Note: This attribute is used by &lt;details&gt; element only. This attribute has been DEPRECATED and is no longer recommended. Applicable: &lt;details&gt;&lt;dialogue
1 min read