The HTML id attribute is used to specify a unique identifier for an HTML element. Only one element can use a specific id value within a webpage.
The id attribute identifies a specific HTML element and can be used by CSS and JavaScript.
<h1 id="myHeader">
Soopro Pathshala
</h1>
<style>
#myHeader{
background-color:skyblue;
color:black;
padding:40px;
text-align:center;
}
</style>
<h1 id="myHeader">
Soopro Pathshala
</h1>
The # symbol is used to select an element by id in CSS.
document.getElementById("myHeader");
JavaScript uses getElementById() to access a specific element.
<a href="#contact"> Go to Contact Section </a> <h2 id="contact"> Contact Us </h2>
IDs can be used to jump directly to sections within a webpage.
| ID | Class |
|---|---|
| Must be unique | Can be reused |
| Uses # selector | Uses . selector |
| Higher CSS priority | Lower CSS priority |
Which symbol is used before an ID selector in CSS?