HTML Id 📌


The HTML id attribute is used to specify a unique id for an HTML element.


Using The id

Ther is some point we need to know about ID

  1. We can use this for one single and particular element. We can't use it twice.
  2. ID Specificity is 1,0,0 (This is hight). In order to replace it we can use an !important
  3. The syntax for id is: write a hash character (#), followed by an id name. Then, define the CSS properties within curly braces {}.
  4. In the following example, we have an <h1> element that points to the id name "myId". This <h1> element will be styled according to the #myId style definition in the head section:

Example:

                
<!DOCTYPE html>
<html>
    <head>
        <style>
            #myId {
                background-color: lightblue;
                color: black;
                padding: 40px;
                text-align: center;
            }
        </style>
    </head>
<body>

<h1 id="myId">My Id test</h1>

</body>
</html>
            

Result:

My Id test

Difference Between Class and ID 👇🏾

A class name can be used by multiple HTML elements, while an id name must only be used by one HTML element within the page:

HTML Bookmarks with ID and Links

  1. HTML bookmarks are used to allow readers to jump to specific parts of a webpage.
  2. can be useful if your page is very long.
  3. To use a bookmark, you must first create it, and then add a link to it.
  4. Then, when the link is clicked, the page will scroll to the location with the bookmark.

Example:

                
<h2 id="myId">Go to page header</h2>
            

Result:

Go to page header

HTML Id Summary ✨