How to Give an Element a Class in HTML
The class attribute puts elements into a named group. One CSS rule for . and that name then styles every element in the group, and one element can belong to several groups.
Syntax
<element class="<class> <class>"><content></element>
| Part | What it does |
|---|---|
element | The tag name of any element, such as li or p. |
class="<class> <class>" | One or more class names, separated by spaces. Any number of elements can share a class. |
<content> | What's inside the element. |
MDN's class page describes the attribute.
The Example
The example is a to-do list of three tasks. The two that are done have class="done", and a CSS rule for .done, in a style element in head, shows them grey and crossed out.
Step 1: Write the Code
Create a file named index.html in a text editor, such as Visual Studio Code:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>To do</title>
<style>
.done {
color: #888888;
text-decoration: line-through;
}
</style>
</head>
<body>
<ul>
<li class="done">Buy milk</li>
<li>Call the bank</li>
<li class="done">Water the plants</li>
</ul>
</body>
</html>
In the CSS, .done with a dot selects the elements whose class is done. In the HTML, the class name has no dot.
Step 2: Open It in a Browser
Double-click index.html. It opens in your browser:

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.
What Happened
- Both items with
class="done"got the.donerule: Chromium reported their color asrgb(136, 136, 136), which is#888888, and their text decoration asline-through. - Call the bank kept the default black, because it has no class.
Common Mistakes
| Mistake | What happens | Do this instead |
|---|---|---|
Writing the dot in the attribute, class=".done" | The rule doesn't match: the text stayed black | Write the dot only in the CSS: class="done" in the HTML, .done in the CSS |
Using different capital letters, such as class="Done" | Class names are case-sensitive, so .done didn't match and the text stayed black | Spell the class the same way in the HTML and the CSS |
Writing two class names with a comma, class="done, urgent" | The comma becomes part of the first name: the element's classes were done, and urgent, so .done didn't match and the text stayed black, while .urgent still made it bold | Separate class names with spaces only |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A .class rule doesn't apply | The class name differs, has a dot, or has different capital letters | Make the name in the HTML match the CSS exactly |
| Only one of two classes applies | The names are separated by something other than a space | Use a space between them |
FAQ
Can an element have more than one class?
Yes. Separate the names with spaces. A paragraph with class="done urgent" got both rules: grey from .done and bold from .urgent.
What's the difference between class and id?
A class can be shared by many elements, and an element can have several. An id names one element only.
Can a script find elements by class?
Yes. document.querySelectorAll('.done') returns every element with the class done: on the example page, the two done items, Buy milk and Water the plants.
Conclusion
A class puts elements into a group that CSS and scripts can reach with . and the name. Write the name without a dot in the HTML, keep its capital letters the same everywhere, and separate several classes with spaces.
Additional Resources
- class global attribute, on MDN.
