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.

Pointidclass
ElementsOnly oneMany
CSS selector#name.name
Many on one elementNoYes, 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
No Content Found.
Exercises & Assignments
No Content Found.
Interview Questions & Answers
No Content Found.