class and id are attributes you add to elements so you can find them later: with CSS to style them, or with JavaScript to make them do something. On their own they don't change how anything looks. They are name tags.
As soon as a page has more than a few elements, you need a reliable way to point at "this paragraph" or "all of these cards". Classes and ids are that way. Almost every CSS rule and most JavaScript code you write will start by selecting elements through them.
The class attribute: a reusable label
- Many elements can share the same class.
- One element can have several classes, separated by spaces.
<p class="note">Remember to save your work.</p>
<p class="note warning">This cannot be undone.</p>In CSS you target a class with a dot: .note { … }. Every element with that class gets the style, so you write the rule once and reuse it as often as you like.
Combining classes is a powerful habit. Above, both paragraphs get the shared note style, and the second also gets the extra warning style.
The id attribute: a unique name
- An
idmust be unique on the page. Only one element can have it. - One element has at most one id.
<section id="pricing">…</section>In CSS you target an id with a hash: #pricing { … }. Ids are also used for jump links (href="#pricing"), for connecting a <label> to its input, and by JavaScript to grab one specific element.
Naming rules and good habits
- Names are case-sensitive:
Cardandcardare different. - A single name can't contain spaces, because a space starts another class.
- Use lowercase words joined by hyphens:
product-card,main-nav. - Name things by what they are, not how they look.
error-messagestays correct if the colour changes from red to orange, butred-textdoesn't. - Prefer classes for styling. Keep ids for jump links, form labels and JavaScript hooks.
Why classes are preferred for styling
Ids have a very high priority in CSS. A rule written for an id beats almost any class rule, which makes styles hard to override later. Ids also can't be reused, so a style written for #promo-box can't be shared with a second box. Classes avoid both problems, which is why most professional stylesheets contain hardly any id selectors. You'll see the reasons in detail in CSS Specificity.
HTML Class and ID Syntax
class and id
<div id="unique-name" class="name another-name">…</div>#unique-name { } /* one element */
.name { } /* every element with that class */HTML Class and ID Summary
classandidlabel elements so CSS and JavaScript can find them.- Many elements can share a class, and one element can have several classes.
- An id must be unique on the page.
- Target classes in CSS with
.nameand ids with#name. - Name classes by purpose, in lowercase with hyphens, and prefer classes for styling.
HTML Class and ID Examples
Each example runs in the page. Edit the code and press Run to see what changes.
Styling with classes
Three cards share card; one also has featured. Add featured to another card and run.
<div class="card">Basic: £0</div>
<div class="card featured">Pro: £9</div>
<div class="card">Team: £29</div>
<style>
.card { padding: 12px 16px; margin: 8px 0; border: 2px solid #ccc; border-radius: 10px; }
.featured { border-color: #17a865; background: #d6f4e4; font-weight: bold; }
</style>Line-by-line explanation
- Lines 1 to 3All three share the
cardclass, so they get the same base style. - Line 2The middle card has two classes:
cardandfeatured. - Line 6
.cardstyles every card. - Line 7
.featuredadds extra style only to cards with that class.
id for a jump link and JavaScript
Click the button: JavaScript finds the element by its id and changes it.
<a href="#booking">Jump to booking ↓</a>
<p style="height: 200px">…</p>
<h2 id="booking">Book a table</h2>
<p id="status">No table booked yet.</p>
<button onclick="document.getElementById('status').textContent = 'Table booked for 7pm.'">Book now</button>Line-by-line explanation
- Line 1The link targets the element with
id="booking". - Line 4A unique id makes this paragraph easy to find from JavaScript.
- Line 5
getElementById('status')finds it and changes its text. You'll learn this in DOM Selecting.
Common HTML Class and ID Mistakes
Using the same id twice
<p id="price">£10</p>
<p id="price">£20</p><p class="price">£10</p>
<p class="price">£20</p>Duplicate ids break jump links, labels and JavaScript, which only ever finds the first one.
Commas or dots in the class attribute
<div class=".card, featured">…</div><div class="card featured">…</div>The dot is only for CSS. In HTML, separate class names with spaces.
HTML Class and ID in Real Projects
Design systems run on classes
Large websites have "design systems": libraries of reusable components like .btn, .btn-primary, .card and .alert. Developers build pages by mixing these classes, just like the card featured example. Frameworks like Tailwind CSS take this further with classes like p-4 and text-center.
HTML Class and ID Exercises
Answer each one and check it instantly. If you get stuck, open the hint before the answer.
Give the paragraph two classes: tip and small.
<p class="">Drink water while you code.</p>
class attribute.How many elements on a page can share the same id?
Which is the best class name for a box that shows form errors?
Both alerts should get the warning style, but they're using the same id and the CSS targets a class. Fix the HTML.
<p id="alert">Low battery</p>
<p id="alert">Storage almost full</p>
<style>
.alert { background: #fff1c2; padding: 8px; border-left: 4px solid orange; }
</style>- Both paragraphs have the class "alert"
- No duplicate ids
- The style is applied
.alert matches the class.HTML Class and ID Quiz
3 questions to check what you learned.
In CSS, how do you select elements with
class="box"?#boxbox*box.box
Can one element have more than one class?
- No, only one
- Yes, separated by commas
- Yes, separated by spaces
- Only if it has no id
Which is a good use for an id?
- Styling every button
- The target of a jump link like
#contact - Styling all paragraphs
- Grouping list items
HTML Class and ID Challenge
Label a menu
Create three <li> items in a <ul id="menu">:
- All three items have the class
item - The second item also has the class
active
<ul>
</ul>- The list has the id "menu"
- Three items with the class "item"
- Only the second item is active
class="item active" gives two classes.