HTML | Id Attributes

The id attribute is a unique identifier which is used to specify the document. It is used by CSS and JavaScript to perform a certain task for a unique element. In CSS, the id attribute is used using # symbol followed by id.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!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>                    

chevron_right


Output:
id attributes



Browsers that support Id attribute:

  • Google Chrome
  • Mozilla
  • Internet Explorer
  • Safari
  • Opera

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.

Difference between id and class:

  • HTML page contains multiple element with same class but id is unique, only single id exist in single page.
  • HTML class use ‘.’ character before the name of the class while id use ‘#’ character before the name of the in CSS.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>class attribute</title>
        <style>
            .gfg {
                color:#009900;
                text-align:center;
            }
        </style>
    </head>
          
    <body>
        <h1 class="gfg">GeeksforGeeks</h1>
        <div class="gfg">A computer science portal for geeks</div
    </body>
</html>                    

chevron_right


Output:
ise selector

Use of ID attributes in JavaScript
In JavaScript, the id attribute is used to manipulate the text, if you want to make changes on a precise element in your script, then you can use id attribute.

Example:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html>
    <head>
        <title>use 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>                    

chevron_right


Output:
id selector



My Personal Notes arrow_drop_up


If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.