HTML id

HTML id attribute specifies unique element within HTML document. Keep in mind that, the same id attribute of an HTML element can not assign to other HTML elements.

Note HTML id attribute specifies a unique HTML element means no other HTML element can put the same id.

<h2 id="main_heading">This is HTML heading tag</h2>

Example

Source Code

          <h2 id="main_heading">This is HTML heading tag</h2>
        
Try it now

Note:Here,HTML element <h2> having unique id(main_heading). That will be unique inside the HTML document.

Writing CSS Style To HTML Id Element

To write a cascade style sheet for the specific Id of the HTML element, write a hash character followed by an id name. Then define the CSS rule inside curly bracket{}.

#main_heading { background-color:orange; color: black; padding: 40px; text-align: center; }

Example

Source Code

          #main_heading {
  background-color: lightblue;
  color: black;
  padding: 40px;
  text-align: center;
}
        
Try it now

Note: The CSS rule has been written with respect to the unique id of the HTML element is only applicable to a single HTML element.

Javascript:Fetching HTML Element Through Id

Example

Source Code

          <script>
function displayResult() {
document.getElementById("main_heading").innerHTML = "You can see the result right now.";
}
</script>
        
Try it now

Note:Javascript is used getElementById() method to fetch HTML element having a unique id.

Difference Between Id And CLass

Example

Source Code

          #main_heading{
  background-color: violet;
  padding: 20px;
  text-align: center;
  color: black;
}
.flower{
  background-color: orange;
  padding: 12px;
  color: black;
}
        
Try it now

Note:An HTML element has a unique id means this id can not assign to other HTML elements while a class to be assigned more than one HTML element.

Online Test / Quiz

Web Tutorials

HTML id
Html Tutorial HTML
Javascript Tutorial JAVASCRIPT
Css Tutorial CSS
Bootstrap 5 Tutorial BOOTSTRAP 5
Bootstrap 4 Tutorial BOOTSTRAP 4