HTML Id and HTML class both are selectors but are different. Now lets discuss HTML Id vs HTML Class below:
HTML Id vs HTML Class:
HTML id | HTML Class |
HTML Id should be unique. | HTML Classes can be similar. |
It is used to identify single element. | Multiple elements can be identified using same class. |
A HTML element can have only one id. | A html element can have more than one class. |
ID attribute starts with (#) symbol. | Class attribute starts with (.) symbol. |
Id is used when we have to use CSS property for a single element. | To use CSS property in multiple places class is used |
#idname | .classname |
<!DOCTYPE html> <html> <head> <style> .food { text-align: center; background-color: blue; } #para{ border: 2px solid black; } </style> </head> <body> <div class="food"> <h2>Class example</h2> <p>The styling of this paragraph is done using class attribute.</p> </div> <p id="para"> This is done using id attribute</p> </body> </html>
Visit difference between ID and Class for more differences.