Back to subject
HTML5/HTML Class and ID

Use Labels Wisely

Practical lesson

Let's use class and id for two different identification jobs.

- class="card" gives an element a reusable group name. Many elements can share the same class. - id="main-card" gives one element a unique identifier within the document. - A class is commonly reused for styling or grouping similar elements. - An id can identify one specific element, and links can target it with a URL fragment such as #main-card. - You can put both class and id on the same element when both kinds of identification are useful. - Keep class names meaningful so another developer can understand what the element represents. - An id should be unique in the document. - Do not create several elements with the same id. - Remember that class and id do not change what the element means semantically; they identify it.

The practical difference is simple: class is reusable, while id is meant to identify one particular element.

Try it yourself
Code

Starter example

Code
<section id="contact" class="card">
  <h2>Contact</h2>
  <p>Email us.</p>
</section>
<a href="#contact">Jump to Contact</a>
Live preview