← Back to HTML & CSS, page 2

html-css-0026 · Easy fresher

How are id and class different in HTML and CSS?

An id names a single element and must be unique in the page. A class names a group, and an element can have several. In CSS, ids are selected with # and have higher specificity, so teams usually style with classes and keep ids for link targets, label connections and ARIA relationships.

Full answer

An id names one element. It must be unique in the page. A class names a group. Many elements can share the same class, and one element can have several classes separated by spaces.

In CSS, an id is selected with # and a class with .. An id selector is more specific than a class selector, so it wins conflicts. That is why most teams style with classes and keep ids for other jobs.

Real uses of an id:

  • A link target. <a href="#fees"> jumps to the element with id="fees".
  • Connecting a label to an input with for.
  • ARIA relationships such as aria-labelledby.
  • A single element picked from JavaScript, for example with document.getElementById.

Two spans share badge and pass, so one rule styles both. The element with id="summary" is unique and is reachable through the link #summary.

When an id is repeated, the HTML is invalid. getElementById returns only the first match, anchors jump to the first, and a label may connect to the wrong input.

html
<style>
  .badge { padding: 2px 8px; border-radius: 4px; color: #fff; }
  .badge.pass { background: seagreen; }
  .badge.fail { background: crimson; }
  #summary { font-weight: bold; }
</style>
<p id="summary">Results</p>
<span class="badge pass" id="b1">Maths</span>
<span class="badge fail" id="b2">Physics</span>
<span class="badge pass" id="b3">Biology</span>
<p><a href="#summary" id="jump">Jump to summary</a></p>
<script>
  document.title = document.querySelectorAll(".badge.pass").length + " passed";
</script>

Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.

Key points

  • id must be unique; class can repeat and be combined.
  • Selectors: #id and .class; id selectors are more specific.
  • Use ids for fragment links, label for and ARIA references.
  • Duplicate ids make getElementById and fragments unreliable.
  • Style with classes to keep specificity low.

Common mistakes

  • copying a block of HTML and forgetting to change its ids, which leaves duplicates.
  • styling with ids and then finding that a later class cannot override them without !important.

Follow-ups

  • can an id contain spaces?
  • Can one element have more than one class?
  • How would you find all elements with a class in JavaScript?

Tips

  • Use classes for styling, ids for targets and relationships. Naming classes after purpose (badge, card) is easier to maintain than naming after look (red-text).
  • State the simple rule: classes for styling, ids for targets.
  • Bring up the specificity side effect; it shows you know why classes are preferred.