HTML Classes
The class attribute gives an element one or more names that CSS and JavaScript can use to find it. Classes are the main way you connect HTML to styles: write a rule once, then apply it to as many elements as you like.
Defining and using a class
In HTML, add class="name" to an element. In CSS, select it with a dot followed by the name.
<style>
.warning {
background-color: #fff3cd;
border-left: 5px solid #f0ad4e;
padding: 10px;
font-family: sans-serif;
}
</style>
<p class="warning">Mind the gap between the train and the platform.</p>
<p>This paragraph has no class, so it looks normal.</p>
<div class="warning">A div can use the same class as a paragraph.</div>The class works on any element type. Every element with class="warning" gets the same look.
Multiple classes on one element
An element can have several classes, separated by spaces. The styles of each class are combined. This lets you build small, reusable pieces and mix them.
<style>
.btn { padding: 8px 16px; border: none; border-radius: 6px; font-size: 15px; cursor: pointer; }
.primary { background: #e34c26; color: white; }
.secondary { background: #e0e0e0; color: #222; }
.large { font-size: 20px; padding: 12px 24px; }
.round { border-radius: 999px; }
</style>
<button class="btn primary">Save</button>
<button class="btn secondary">Cancel</button>
<button class="btn primary large">Big save</button>
<button class="btn secondary round">Round cancel</button>If two classes set the same property, the rule that comes later in the CSS wins (when both have equal specificity) — not the order inside the class attribute.
Combining classes with element types
You can narrow a selector by writing the tag name before the dot, or require two classes at once by chaining them with no space.
| Selector | Matches |
|---|---|
.note |
Any element with class note |
p.note |
Only <p> elements with class note |
.note.important |
Elements that have both classes |
.card .title |
Elements with class title inside a .card |
<style>
body { font-family: sans-serif; }
.note { color: #2a6; }
p.note { font-style: italic; }
.note.important { font-weight: bold; color: #c00; }
.card .title { text-transform: uppercase; letter-spacing: 1px; }
</style>
<p class="note">p.note: green and italic.</p>
<span class="note">span.note: green but not italic.</span>
<p class="note important">Both classes: bold red (and italic, because it's a p).</p>
<div class="card"><h3 class="title">Inside a card</h3></div>
<h3 class="title">Outside a card</h3>Classes in JavaScript
JavaScript can find elements by class and add, remove or toggle classes. Toggling a class is the cleanest way to change how something looks: the styling stays in CSS, and the script just flips a switch.
<style>
body { font-family: sans-serif; transition: background 0.3s; }
.dark { background: #1e1e1e; color: #eee; }
.item.done { text-decoration: line-through; color: gray; }
</style>
<button id="theme">Toggle dark mode</button>
<ul>
<li class="item">Buy milk</li>
<li class="item">Walk the dog</li>
<li class="item">Write HTML</li>
</ul>
<p>Click a task to mark it done.</p>
<script>
document.getElementById('theme').addEventListener('click', () => {
document.body.classList.toggle('dark');
});
for (const li of document.getElementsByClassName('item')) {
li.addEventListener('click', () => li.classList.toggle('done'));
}
</script>| JavaScript | What it does |
|---|---|
document.getElementsByClassName('item') |
Live collection of elements with that class |
document.querySelectorAll('.item') |
Static list of matches, using a CSS selector |
el.classList.add('x') |
Adds a class |
el.classList.remove('x') |
Removes a class |
el.classList.toggle('x') |
Adds it if missing, removes it if present |
el.classList.contains('x') |
true or false |
Naming tips
- Class names are case-sensitive:
.Boxand.boxare different. - They can't contain spaces (a space separates two classes) and shouldn't start with a digit.
- Name classes by purpose, not appearance:
.error-messagekeeps making sense even if you change it from red to orange;.red-textdoesn't. - Use a consistent style such as kebab-case:
.product-card,.nav-link.
Tip: Need to style exactly one unique element? You can use an
id, but a class is usually still the better choice — it keeps specificity low and the style reusable.
Test yourself
How do you select elements with class="menu" in CSS?
#menu.menumenu*menu
How do you give an element two classes, card and featured?
class="card, featured"class="card" class="featured"class="card featured"class="card.featured"
Which method adds a class if it's missing and removes it if present?
classList.switch()classList.toggle()classList.flip()classList.replace()