Enter your email id to get regular update free
Login
Your Email
Pasword

What is the id Attribute in HTML?

The id attribute specifies a unique id for an HTML element .

The id value can be used by CSS and JavaScript to perform certain tasks for a unique element with the specified id value.
To select an element in css with a specific id you need to write a hash (#) character followed by the id of the element.
E.g:
<style>
#mycode {
 background-color: blue;
 color: White;
 padding: 30px;
 text-align: center;
}
</style>
<h1 id="myHeader">My Header</h1>


HTML element can only have one unique id that belongs to that single element while a class name can be used by multiple elements.
id Attribute in JavaScript
JavaScript can access an element with a specified id by using the getElementById() method.
E.g:
<script>
function displayResult() {
document.getElementById("mycode").innerHTML = "Have a nice nice coding";
}

The id value should contain at least one character and  not contain whitespace.
id Attribute
JavaScript
HTML
Ayesha
Tech writer at newsandstory
Total 159 views