Skip to main content

How to Give an Element a Class in HTML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

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​

Syntax
<element class="<class> <class>"><content></element>
PartWhat it does
elementThe 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:

index.html, an HTML page with a style element whose rule for .done sets a grey color and a line-through, and a list of three items: Buy milk and Water the plants with the class done, and Call the bank without a class.

index.html
<!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 page in Chromium: a bulleted to-do list where Buy milk and Water the plants are grey and crossed out, and Call the bank is in normal black text.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • Both items with class="done" got the .done rule: Chromium reported their color as rgb(136, 136, 136), which is #888888, and their text decoration as line-through.
  • Call the bank kept the default black, because it has no class.

Common Mistakes​

MistakeWhat happensDo this instead
Writing the dot in the attribute, class=".done"The rule doesn't match: the text stayed blackWrite 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 blackSpell 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 boldSeparate class names with spaces only

Troubleshooting​

SymptomLikely causeFix
A .class rule doesn't applyThe class name differs, has a dot, or has different capital lettersMake the name in the HTML match the CSS exactly
Only one of two classes appliesThe names are separated by something other than a spaceUse 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​