×

HTML Id Attribute

The HTML id attribute represents the unique identifier for an element of the HTML document. Each element has its unique identifier.

The id attribute is a global attribute that can be used by an HTML element. The value of the id attribute should be unique. The id attribute can be used to style webpages. It takes the value, which must be relevant and a unique identifier. CSS and JavaScript use the id attribute to perform the tasks meant for the unique id element.

Syntax:

<tag id="identifier" />

Examples of HTML id attribute:

Example 1: Id attribute on the body element

This example demonstrates the id attribute applied to the body element. We give the unique id to the body element. See the code below:

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on Body Element </title>
    <style>
    #body-element{
        background-color: lightgreen;
    }    
    </style>
</head>
<body id="body-element">
  <p> We are applying CSS to the body element using id attribute. </p>  
</body>
</html>

Output:

As you can see in the output, the unique id is given to the body element which has been used to apply the background color in the body tag.

HTML Id Attribute

Example 2: Executing JavaScript function using the id attribute

This example shows the use of the id attribute to execute JavaScript. We are adding a script inside the body element.

<!DOCTYPE html>
<html>
<head>
    <title>Applying JavaScript to div element using Id Attribute</title>
    <style>
    #text {
        font-size: 50px;
        color: grey;
        font-weight: bold;
        margin-bottom: 30px;
    }
    </style>
</head>
<body>
    <div id="text"> TutorialAndExamples </div>
    <button onclick="changetext()"> Display Text Change </button>
    <script>
    function changetext() {
        document.getElementById("text").innerHTML = 
        "Welcome to TutorialAndExample.";
        document.getElementById("text").style.color = "red";
    }
    </script>
</body>
</html>

Output:

When you click on the display change button in the output, the JavaScript will change the text and the color of the text. The color of the text turns into red.

HTML Id Attribute

Example 3: Id attribute on <a> tag

In this example, <a> tag is used to link one web page to another. A unique id is provided to the <a> tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on anchor tag </title>
    <style>
    #google-link{
       color: rgb(41, 163, 41);
       font-weight: bold;
       font-size: 50px;
    }    
    </style>
</head>
<body>
    <p> Below is a hyperlink. </p>
    <a id="google-link" target="_blank" 
    href="https://www.google.com/"> Google </a> 
</body>
</html>

Output:

In the output, green text is a hyperlink that will take you to another web page.

HTML Id Attribute

Example 4: Id attribute on <abbr> tag

In this example, <abbr> tag is used to write a short form of words. A unique id is provided to the <abbr> tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on abbreviation element </title>
    <style>
    #isro-abbr{
       color: rgb(222, 112, 28);
    }    
    </style>
</head>
<body>
    <p>
        Abbreviations are used to write words in short form. <br> <br>
        Indian Space Research Organization: <abbr id="isro-abbr" title="Indian Space Research Organization">ISRO</abbr>
     </p> 
</body>
</html>

Output:

The output shows the abbreviation in orange color.

HTML Id Attribute

Example 5: Id attribute on <address> tag

We use the address tag to provide contact information. <Address> Tag is a specified unique identifier.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on abbreviation element </title>
    <style>
    #company-address{
       color: rgba(173, 178, 19, 0.694);
    }    
    </style>
</head>
<body>
    <p>
        Usually, we write the company address in the footer. 
    </p> 
     <footer>
        <address id="company-address">
          Contact: <a href="techno@company.com"> Ronald Jackson </a>, @ronald_jackson
        </address>
      </footer>
</body>
</html>

Output:

With the help of a unique id, we have added CSS to the <address> tag.

HTML Id Attribute

Example 6: Id attribute on <article> tag

We use <article> tag to article in HTML document.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on article element </title>
    <style>
    #article{
       color: rgba(179, 251, 173, 0.694);
       background-color: black;
    }    
    </style>
</head>
<body>
<article id="article">
    <h2> Article </h2>
    <p> When we need to write an article in HTML document, we use the article element. The article element tells the programmer and browser that this piece of text is an article. </p>
</article>
</body>
</html>

Output:

The output shows an article with a unique id to style the article element.

HTML Id Attribute

Example 7: Id attribute on <aside> tag

We have give a unique id to <aside> tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on aside element </title>
    <style>
    #programming{
        display:flex;
    }
    #languages-aside{
       margin: 60px; 
       padding: 15px; 
       border-left: 4px solid; 
       color: rgb(11, 81, 135);
    }    
    </style>
</head>
<body>
<div id="programming">
    <main>
        Programming languages: 
        <ul>
          <li> Java </li>
          <li> C </li>
          <li> C++ </li>
          <li> Python </li>
          <li> JavaScript </li>
          <li> HTML </li>
          <li> CSS</li>
        </ul>
      </main>
      
      <aside id="languages-aside">
       <i> Learn programming languages from best institute. </i>
      </aside>
     </div>
</body>
</html>

Output:

The output shows the aside element in blue color.

HTML Id Attribute

Example 8: Id attribute on <bold> tag

We are using a unique id for the bold element.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on bold element </title>
    <style>


    </style>
</head>
<body>
<p> This is a <b id="bold-text"> bold </b> text. </p>
</body>
</html>

Output:

The output shows the bold text.

HTML Id Attribute

Example 9: Id attribute on <button> tag

This example displays the button element with its unique identifier.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on button element </title>
    <style>
#alert-button{
    color: red;
    background-color: yellow;
}
    </style>
</head>
<body>
    <button id="alert-button"
    onclick="alert('The request has been approved!');">Click here to approve</button>
</body>
</html>

Output:

The output shows the button with a yellow background and red text. If you click this button, then it will show an alert message.

HTML Id Attribute

Example 10: Id attribute on <form> tag

We are providing unique identifier to <form> tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on form element </title>
    <style>
#Shampoo-form{
    color: rgb(175, 29, 233);
    background-color: rgb(243, 243, 237);
}
    </style>
</head>
<form id="Shampoo-form" action="#">
    <fieldset>
      <legend> Shampoo Details </legend>
  
      <input type="text" placeholder="Company" name="Company"><br /><br />
      <input type="text" placeholder="Ingredients" name="Ingredients"><br /><br />
      <input type="text" placeholder="Price" name="Price"><br /><br />


      <button style="background-color: yellow; color: rgb(237, 109, 237);" type="submit">Submit</button>
    </fieldset>
  </form>
</body>
</html>

Output:

With the help of the id attribute, CSS is applied to the form element.

HTML Id Attribute

Example 11: Id attribute on <iframe> tag

The <iframe> tag embeds another web page in the current HTML page.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on Inline Frame Element </title>
    <style>
    #tutorial{
        height:650px;
        width:100%;
    }  
    </style>
</head>
<body>
    <iframe id="tutorial" src="https://www.tutorialandexample.com/">
</iframe> 
</body>
</html>

Output:

The output shows another web page in the current HTML page.

HTML Id Attribute

Example 12: Id attribute on <image> tag

We use <image> tag to insert images in the HTML document. We are giving unique id to <image> tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on Image Element </title>
    <style>
    #background{
        height: 200px;
        width:50%;
    }  
    </style>
</head>
<body>
    <img id="background"
    src="background.jpg"
    alt="Background Image">
</body>
</html>

Output:

Using the id attribute, we have applied height and width to the <image> tag.

HTML Id Attribute

Example 13: Id attribute on <optgroup> tag

This example demonstrates the <optgroup> tag with a unique id.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on Optgroup Element </title>
    <style>
    #Indian-food{
        color: brown;
    } 
    #Chinese-food{
        color: rgb(222, 62, 217)
    }  
    </style>
</head>
<body>
    <select>
        <option value=""> Select food </option>
        <optgroup id="Indian-food" label="Indian">
          <option value="Chole Bhature"> Chole Bhature </option>
          <option value="Dosa"> Dosa </option>
          <option value="Idli"> Idli </option>
        </optgroup>
        <optgroup id="Chinese-food" label="Chinese">
            <option value="Chilly Potato"> Chilly Potato </option>
            <option value="Momos"> Momos </option>
            <option value="Noodles"> Noodles </option>
          </optgroup>
      </select>
</body>
</html>

Output:

The output shows the two groups which are created using the <optgroup> tag in the code. And, we have applied CSS to both groups using the id attribute.

HTML Id Attribute

Example 14: Id attribute on <q> tag

This example displays the inline quotation that has a unique id.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on Quotation Element  </title>
    <style>
    #quote{
        color: mediumvioletred;
    }  
    </style>
</head>
<body>
    <p>
        <q id="quote">
            Talk to yourself once in a day, otherwise you may miss meeting an intelligent person in this world.
        </q><br />
        - Swami Vivekananda
      </p>
</body>
</html>

Output:

The output shows the quotation, and we use the id attribute to provide a style to the <q> tag.

HTML Id Attribute

Example 15: Id attribute on <span> tag

This example displays the span element, which has a unique identifier.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on Span Element </title>
    <style>
    #text-part{
        color: orange;
        background-color: rgb(51, 14, 172);
    }  
    </style>
</head>
<body>
    <p>
        The span is an inline element. We use the span element to apply a style to <span id="text-part"> some part of the text </span> in a paragraph.
    </p>
</body>
</html>

Output:

The output shows the span element on which we have implemented CSS using the unique id of the <span> tag.

HTML Id Attribute

Example 16: Id attribute on <table> tag

This example demonstrates the table, which has a unique identifier.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on Table Element  </title>
    <style>
    #table{
            border-collapse: collapse;
            width: 24px;0px;
    } 
    th, td{padding: 5px; border: solid 1px rgb(131, 32, 32); 
    }
    th{
        background-color: rgb(241, 142, 127);
    }
    </style>
</head>
<body>
    <table id="table">
        <tr>
          <th> Student name </th>
          <th> Subject </th>
          <th> Status </th>
        </tr>
        <tr>
          <td> Kristen </td>
          <td> English </td>
          <td> Pass </td>
        </tr>
        <tr>
          <td> Daniel</td>
          <td> Maths </td>
          <td> Pass </td>
        </tr>
      </table>
</body>
</html>

Output:

This output shows the table on which the style is applied using the id attribute.

HTML Id Attribute

Example 17: Id attribute on <table> tag

This example demonstrates the unordered list, which has its unique id.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on Unordered List Element </title>
    <style>
    #color{
        color: mediumvioletred;
        background-color: aquamarine;
    }  
    </style>
</head>
<body>
    <div> <b> Colors: </b> </div>
    <ul id="color"> 
        <li> Red </li>
        <li> Yellow </li>
        <li> Blue </li>
        <li> Green </li>
        <li> Orange </li>
        <li> Pink </li>
        <li> Purple</li>
        <li> Black </li>
        <li> Grey </li>
        <li> Brown </li>
        <li> Violet </li>
    </ul>
</body>
</html>

Output:

The output shows the list of colors. We have applied the style to the list with the help of the id attribute.

HTML Id Attribute

Example 18: Id attribute on <table> tag

This example demonstrates the use of the id attribute on label and textarea elements. Both label and textarea have unique ids.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on label and textarea Element  </title>
    <style> 
    #about{
        color: rgb(192, 75, 75);
        background-color: ;
    }
    #about-yourself{
        background-color: bisque;
    }
    </style>
</head>
<body>
    <label id="about" for="about yourself"> About Yourself </label><br/>
    <textarea id="about-yourself" rows="6" cols="50"
              placeholder="Write about yourself">
    </textarea>
</body>
</html>

Output:

The output shows the label and text area, which have their unique identifier. We have used the id attribute to apply the style.

HTML Id Attribute

Example 19: Id attribute on <section> tag

We use <section> tag to divide the content into sections and subsections. We are creating two sections with their unique identifiers.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on Section Element </title>
    <style> 
    #section1{
        background-color: rgb(226, 193, 193);
    }
    #section2{
        background-color: rgb(232, 130, 130);
    }
    </style>
</head>
<body>
    <section id="section1">
        <h2> Section 1 </h2>
        <p>
            This is paragraph 1
        </p>
    </section>
    <section id="section2">
        <h2> Section 2 </h2>
        <p>
            This is paragraph 2
        </p>
    </section>
</body>
</html>

Output:

As you can see, there are two sections, and we have applied styles to them using the id attribute.

HTML Id Attribute

Example 20: Id attribute on <sub> and <sup> tag

We are providing unique ids to <sub> and <sup> tag.

<!DOCTYPE html>
<html lang="en">
<head>
    <title> Id Attribute on Subscript and Superscript Element </title>
    <style> 
    #sub2{
        color: rgb(232, 130, 130);
    }
    #sup2{
        color: rgb(232, 130, 130);
    }
    </style>
</head>
<body>
<div> 
    <h2> Subscript </h2> 
    <p> The chemical formula of water is H<sub id="sub2">2</sub>O. </p>
</div>
<div> 
    <h2> Superscript </h2> 
    <p> This is an algebraic expression: <br> 2x<sup id="sup2">2</sup> + 4x </p>
</div>
</body>
</html>

Output:

The output shows subscript and superscript elements.

HTML Id Attribute

In all the above examples of HTML elements, we have used the id attribute to provide the identifier to all the elements. We have provided the styles to the elements with the help of the id attribute.

We have executed JavaScript with the help of the id attribute. We can apply the id attribute to any HTML element.

Browser Support:

Id Attribute supports the following browsers:

  1. Google Chrome
  2. Internet Explorer
  3. Safari
  4. Mozilla Firefox
  5. Opera

Related Topics

How to Create a Portfolio Gallery using HTML and CSS?

First, before starting this, the user should know some of the basics of HTML and CSS to create a portfolio gallery. To add more functionality to the website, we can also...

7 minutes read.

HTML Date

The input element in HTML has several attributes. One of its attributes is the type attribute which creates a calendar in an order that a user can select a date...

5 minutes read.

HTML Form Input Types

HTML Form Input Types An important element of the HTML form is < input type= "> in HTML. The type attribute of the input element can be of different types, which...

8 minutes read.

HTML Form Action

The <form> tag has an attribute named action that contains a URL. The HTML action attribute defines the webpage URL on which you want to send the data after submitting...

4 minutes read.

How to add JavaScript to HTML

JavaScript is one of the most important languages today. It is a high-level, interpreted, lightweight programming language that makes web pages more interactive. It is a language of the web....

5 minutes read.

HTML <svg> tag

SVG is used to determine the vector-based graphics in XML format. What is SVG? We utilize SVG tag to specify visuals for the website. The attribute of SVG in a HTML document...

2 minutes read.

Html &lt;dd> Tag

In an HTML description list, an item's definition or description is indicated using the definition description ("dd" tag). A <dd> tag can contain paragraphs, line breaks, images, links, and lists. It...

4 minutes read.

HTML Forms

Forms are required when we want to collect data from the user. Html forms consists of different fields to take user inputs such as: input, text area, checkbox, password, radio...

4 minutes read.

HTML Attributes

HTML Attributes provide the additional information about HTML elements. Attributes are always specified in the start tag. Attributes usually comes in pairs. lang Attribute This attribute helps in declaring the language...

2 minutes read.

What is a Container tag?

In HTML, there are different types of tags or say instructions which make the developer specify various elements in the document and make the browsers understand what to display. Tags in...

5 minutes read.

HTML Youtube

<object> element: It embed the object within the HTML documents and embed plug-ins such as Java applets, PDF readers, Flash Players etc in web pages. Example: <object width="100" height="100" data="bookmark.swf"></object> <embed> element: It defines an embedded...

1 minute read.

How to insert image in HTML

An image (technically digital image) in HTML or even in general technical aspects is a binary representation of visual information. By visual information we mean, in the form of drawings/pictures,...

5 minutes read.

HTML 5 Tutorial

What is HTML5? HTML5 is a programming language which stands for the acronym Hyper Text Markup Language 5. It is program that enables the appearance of web pages to be changed, as well as changes...

5 minutes read.

HTML Data Tag

HTML Data Tag: The HTML < data > tag is used to transmit its content to a machine-readable version. The data are presented in a particular format. It is useful...

3 minutes read.

HTML Anchor

HTML Anchor The HTML anchor tags define a hyperlink that links to other pages on one page. It can create hyperlinks to any web page and files, locations or any other...

3 minutes read.

Difference between HTML and DHTML

Introduction to HTML HTML stands for Hypertext Markup Language. It is a standard web language used to create web pages and applications. HTML is used to define the structure and content...

4 minutes read.

HTML <style> tag

The section of information that we want to style is contained in html style tag. It includes CSS, which is used to design the text and images in the web page. Syntax: <head> <title>…………....

3 minutes read.

HTML <address> Tag

Hyper Text Markup Language, HTML is the best markup language for creating Web pages. HTML consists of a wide variety of elements. In all of these elements, one element is...

4 minutes read.

Html <colgroup> Tag

A HTML table's column group is specified by the "colgroup" tag. If you want to apply various properties to one or more <col> elements in an HTML table, then you...

4 minutes read.

HTML Canvas

HTML Canvas: Using JavaScript, the < canvas > tag in HTML is used for drawing graphics on web page. It can be used to draw routes, boxes, text, gradient, and...

3 minutes read.