Skip navigation
Tyssen Design — Brisbane Freelance Web Developer
(07) 3300 3303

What’s the difference between classes and IDs?

By John Faulds /

Classes and IDs are how we create different styles for different elements on the page. IDs are unique identifiers and can only be used once on any one page. Classes can be reused and applied to different elements.

So, for example, this is wrong:


<div id="content">
  Some content here
</div>

<div id="content">
  Some different content here
</div>
				

But this would be right:


<div class="content">
  Some content here
</div>

<div class="content">
  Some different content here
</div>
				

You can also assign classes to different elements:


<div class="content">
  Some content here
</div>

<h3 class="content">Some different content here</h3>
				

(How useful this is would depend on the styles attached to the classes.)

IDs are identified in CSS by # in front of the name, while classes have . in front of them, e.g.:


#myDiv /* this is an ID */
.myDiv /* this is a class */