Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Grouping and Attributes  ›  Lesson

id and class

id 12 minAn id names exactly one element on a page and is used for links and scripts, while a class labels any number of elements that share a role or style.

id and class

Once you have elements on a page, you need ways to point at them: for a style, a script or a link. HTML gives you two naming attributes that every element can use. They look similar, but one is a name and the other is a label.

The rule

id is a name for one; class is a label for many.

id gives an element a name that must be unique on the page. No two elements may share it.

<h2 id="opening-hours">Opening hours</h2>

Uses of an id:

  • Links to a spot on the page: <a href="#opening-hours"> jumps here.
  • Connecting elements: a form label points at its input with for="email", which must match the input's id.
  • Scripts that need to find one specific element.

class labels an element as a member of a group. Any number of elements can share a class, and one element can have several classes, separated by spaces:

<article class="card">...</article>
<article class="card featured">...</article>
<article class="card">...</article>

Classes are the everyday hook for CSS: style .card once and every card follows.

Naming tips: use lowercase words joined by hyphens (opening-hours, price-tag), no spaces inside a single name, and names that describe what the thing is (warning) rather than how it looks today (red-text). Looks change; roles last. Names are case-sensitive, so Card and card are different.

The shortcut

Ask "Could there ever be two of these on this page?" If never, id is allowed. If there could be, use a class.

Naming parts of a blog post pageStep 1 of 4
  1. The comments section

    There is only one per page, and links jump to it: id="comments".

The trap is reusing an id for several elements because it "works" in CSS. Links, labels and scripts expect one match, and duplicates break them in confusing ways.

Both attributes are global: every HTML element accepts them, from p to img to li. That also means you do not need to wrap something in an extra div just to give it a class. Put the class straight on the element that needs it.

Drills

Answer fast — trust the rule.

quick drill

How many elements on one page may share id="menu"?

quick drill

How do you give an element two classes, btn and large?

quick drill

Which name follows good practice for a class on warning messages?

quick drill

A form's <label for="phone"> should connect to its input. What must the input have?

Remember

  • id names one unique element; class labels any number, and one element can have many classes.
  • Shortcut: "could there ever be two?" Yes means class.
  • Trap: duplicate ids, and class names that describe looks instead of roles.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainUnique name or group label?

A product page has one main search box and twelve product cards, three of which are on sale. Explain how you would use id and class on these elements and why.