About Lesson
HTML Id
In this tutorial, we will learn about id in HTML with the help of examples.
Id
TheĀ idĀ attribute is used to specify a unique id for an element in an HTML document.
It is used by CSS and JavaScript to perform certain tasks for a unique element.
We cannot use more than one element with the same id in an HTML document.
The value of theĀ idĀ attribute must be unique within the HTML document.
Using Id Attribute
An Id name can be defined within theĀ <style>Ā element, or in the external CSS file.
The id name isĀ case sensitive.
To create a id, write a hashĀ #Ā character followed by a id name.
Syntax :
#idName
Example :
<!DOCTYPE html>
<html>
Ā Ā <head>
Ā Ā Ā Ā <title>HTML Id</title>
Ā Ā Ā Ā <style>
Ā Ā Ā Ā Ā Ā #cricket {
Ā Ā Ā Ā Ā Ā Ā Ā color : blue;
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā Ā Ā #football {
Ā Ā Ā Ā Ā Ā Ā Ā color : green;
Ā Ā Ā Ā Ā Ā }
Ā Ā Ā Ā </style>
Ā Ā </head>
Ā Ā <body>
Ā Ā Ā Ā <p id = "cricket">This is a paragraph with cricket id.</p>
Ā Ā Ā Ā <p id = "football">This is a paragraph with football id.</p>
Ā Ā </body>
</html>
Output
This is a paragraph with cricket id.
This is a paragraph with football id.