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.

html
<!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.

The page "My recipes" with two recipes, each with its heading and cooking time, and a final note about the hot soup. The classes cause no visible change.

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":

css
.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.

html
<p class="note warning">The soup is very hot.</p>

Choosing good class names

  • Describe what it is, not how it looks. warning is good; red-text is 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, recipe and card.
  • Class names are case-sensitive: Warning and warning are different.

Try it yourself

  1. Select the HTML example and choose Try it.
  2. Add a third recipe card that uses the same classes.
  3. 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 as highlight.
  • Writing class twice on one element, such as class="note" class="warning". Only the first counts. Fix: class="note warning".

Check yourself

  1. How many classes does class="card featured" give an element?
  2. Is bold-red a good class name for a warning? Why?
  3. Can two elements share the same class?

Answers

  1. Two: card and featured.
  2. No. Name the purpose (warning), not the look.
  3. 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.

Chat with us on WhatsApp