Skip to content
FORGENO ADS GAMESUnlock all $8

HTML · Unit 2: Images and page structure · Lesson 4 of 5

Class and id

Any element can have a class attribute, a name you choose that groups elements: class="price".

Opens this lesson in Code Academy. Lessons open one after another, and each unit opens when you pass the checkpoint quiz before it (you can take a checkpoint early to skip ahead). If this lesson is not open yet, the button shows you its unit on your map.

The idea

Any element can have a class attribute, a name you choose that groups elements: class="price". Many elements can share a class, and one element can have several, separated by spaces.

An id names exactly one element on the page, like id="contact". Every id must be unique.

Choose names that describe what something is, not how it looks. class="warning" still makes sense after you change its color; class="red-text" does not.

Worked example

HTML

<p class="note">Bring a jacket.</p>
<p class="note important">The trail closes at dusk.</p>
<h2 id="faq">Questions</h2>

Classes and ids do not change the look on their own. CSS uses them to pick elements.

Key terms

class
An attribute that groups elements under a reusable name. Many elements can share it.
id
An attribute that names exactly one element on the page. It must be unique.

Practise it in Code Academy

It has 4 exercises (multiple choice, fill in the blank, fix the broken code and write code from a prompt) and takes about 6 minutes. Every answer is checked as you go, and feedback names the exact mistake. Hints and the solution are there if you get stuck.

Every lesson is free: no ads, no lives and no paywall.