What is the difference between id and class in HTML?
An id must be unique, so it identifies only one element on the page. A class can be used on many elements, and one element can have many classes.
| Point | id | class |
|---|---|---|
| Elements | Only one | Many |
| CSS selector | #name | .name |
| Many on one element | No | Yes, separated by a space |
<style>
#title { color: navy; }
.note { background: #fff3cd; }
</style>
<h1 id="title">Heading</h1>
<p class="note">Note one</p>
<p class="note">Note two</p>
An id is also used as the target of a link (#id). See HTML Class and ID.
Learn more about the similar topics:
| Tutorials |
|---|
| List of common HTML Tags |
| HTML Elements and Tags |
| HTML Attributes |
| HTML Block and Inline Elements |
| HTML Div and Span |
| HTML Class and ID |
| Exercises & Assignments |
|---|
| No Content Found. |