Lesson 30 of 51 · Module 5, lesson 3 of 7 · 3 min
Class names
Name groups of elements with classes, ready for CSS, and choose names that describe their purpose.
What you'll learn
- How to give elements a
class- How to give one element several classes
- How to choose good class names
Why this matters
When you start CSS, you will say things like "every recipe card gets a light background" or "warnings are red". A class is the name that connects your HTML to those rules. Choosing clear names now makes the CSS course much easier.
The class attribute
A class is a name you choose, written in the class attribute. Many elements can share the same class.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Recipes</title>
</head>
<body>
<h1>My recipes</h1>
<div class="recipe-card">
<h2>Tomato soup</h2>
<p class="cooking-time">30 minutes</p>
</div>
<div class="recipe-card">
<h2>Green salad</h2>
<p class="cooking-time">10 minutes</p>
</div>
<p class="note warning">The soup is very hot when it leaves the pot.</p>
</body>
</html>
What you'll see: nothing different yet. Classes are names; on their own they change nothing on the page.
A preview of what classes are for
This is CSS, which you will learn in the next course. You do not need to understand it yet — only notice how it uses the class names. A dot . means "elements with this class":
.recipe-card {
border: 1px solid #ccc;
}
.warning {
color: darkred;
}
With these two rules, every recipe card gets a border, and the warning turns dark red — without changing one line of HTML.
Several classes on one element
Separate class names with a space. The last paragraph above has two classes: note and warning. It gets the styles of both.
<p class="note warning">The soup is very hot.</p>
Choosing good class names
- Describe what it is, not how it looks.
warningis good;red-textis not — one day the warning may be orange. - Lower case, words joined with hyphens:
recipe-card,cooking-time. - No spaces inside a name. A space starts a second class:
class="recipe card"is two classes,recipeandcard. - Class names are case-sensitive:
Warningandwarningare different.
Try it yourself
- Select the HTML example and choose Try it.
- Add a third recipe card that uses the same classes.
- Give the heading of each card a class, such as
recipe-title.
Common mistakes
- A space inside one name, such as
class="recipe card". It becomes two classes. Fix:recipe-card. - Names that describe the look, such as
big-blue. Fix: name the purpose, such ashighlight. - Writing
classtwice on one element, such asclass="note" class="warning". Only the first counts. Fix:class="note warning".
Check yourself
- How many classes does
class="card featured"give an element? - Is
bold-reda good class name for a warning? Why? - Can two elements share the same class?
Answers
- Two:
cardandfeatured. - No. Name the purpose (
warning), not the look. - Yes. That is what classes are for.
Next: a class can be shared. Sometimes you need a name that belongs to exactly one element — that is the id attribute.

