HTML Id Attribute
HTML id attribute specifies unique element within HTML document.Keep in mind that,same id attribute of HTML element can not assign to other HTML element.
HTML id attribute specifies unique element within HTML document.Keep in mind that,same id attribute of HTML element can not assign to other HTML element.
Note HTML id attribute specifies unique HTML element means no other HTML element can put same id.
General Syntax
<h3 id="main_heading">Heading tag having id attribute.</h3>
Source Code
<p id="main_heading">HTML heading element having id attribute.</p>
Source Code: Output
HTML heading element having id attribute.
Code Explanation
Note:Here,HTML element <h2>
having unique id(main_heading). That will be unique inside the HTML document.
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{}.
General Syntax
<style type="text/css">
#main_heading {
background-color:orange;
color: black;
padding: 40px;
text-align: center;
}
</style>
Source Code
<style type="text/css">
#main_heading {
background-color:orange;
color: black;
padding: 40px;
text-align: center;
}
</style>
Source Code: Output
Code Explanation
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.
General Syntax
<script type="text/javascript">
function displayResult() {
document.getElementById("main_heading").innerHTML = "You can see the result right now.";
}
</script>
Source Code
<script type="text/javascript">
function displayResult() {
document.getElementById("main_heading").innerHTML = "You can see the result right now.";
}
</script>
Source Code: Output
Code Explanation
Note:Javascript is used getElementById() method to fetch HTML element having a unique id.
General Syntax
<style type="text/css">
/* Style the element with the id "myHeader" */
#main_heading{
}
/* Style all elements with the class name "city" */
.flower{
}
</style>
Source Code
<style type="text/css">
/* Style the element with the id "myHeader" */
#main_heading{
background-color: violet;
padding: 20px;
text-align: center;
color: black;
}
/* Style all elements with the class name "city" */
.flower{
background-color: orange;
padding: 12px;
color: black;
}
</style>
Source Code: Output
Code Explanation
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.