An HTML id attribute defines an identifier which is used to uniquely identify HTML element.
The id attribute is defined in the <style> tag using # symbol followed by id.
The id must be unique in a whole document and once it used as an id attribute of an HTML element it can not be used in any other element.
Explanation of the above example:
In this example, we have declared two id’s with the name “#menu1” and “#menu2” respectively in the style tag.
Then these id’s are used with the HTML elements. The “#menu1” id is used with <h2> tag and “#menu2” is used with <h3> tag. The properties defined inside the id are applied to the particular element on which it is used.
Note: In HTML5, the id attribute can be used by an HTML tag but the id attribute can not be used in HTML 4.01 because there are some restrictions to use an id attribute in HTML 4.01. The id attribute can not be used by <base>, <head>, <html>, <meta>, <param>, <script>, <style>, and <title> tag. In HTML 4.01 id can’t start with a number.
Difference between HTML Class and HTML ID:
The difference between the class and the id is that “id” is unique that can be applied to a single element while the “class” name can be be used by multiple elements.