Skip to main content

How to Select Elements by Class in CSS

· 3 min read
Jagdish Kumawat
Founder @ Dewiride

A class selector styles every element that has a class: a dot, then the class name. .note matches every element with class="note", whatever kind of element it is.

Syntax​

Syntax
.<class name> {
<declarations>
}
PartWhat it does
.Marks a class selector.
<class name>The class from the HTML class attribute, such as note, without quotes.
<declarations>The styles every element with the class gets.

MDN's Class selectors page explains that the selector matches elements based on the contents of their class attribute.

The Example​

The example is three short notices. The last two have class="note", and one rule colors them blue.

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 holding a rule for the class note that sets the color 0f6cbd, and three paragraphs, the last two with the class note.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Class selector</title>
<style>
.note {
color: #0f6cbd;
}
</style>
</head>
<body>
<p>The office opens at 9:00.</p>
<p class="note">Bring your badge.</p>
<p class="note">Visitors sign in at reception.</p>
</body>
</html>

.note matches both paragraphs with class="note", and its declaration colors them #0f6cbd.

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: The office opens at 9:00. in black, then Bring your badge. and Visitors sign in at reception. in blue.

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

What Happened​

  • Both notes are blue. Chromium gave them the color rgb(15, 108, 189), which is #0f6cbd.
  • The first paragraph stayed black, because it has no class.

Common Mistakes​

MistakeWhat happensDo this instead
Leaving out the dot: noteWithout the dot, it's a selector for elements named note, so nothing changedStart the selector with a dot
Changing the case: .Note for class="note".Note matched nothing, because the case must matchCopy the class name exactly

Troubleshooting​

SymptomLikely causeFix
A rule such as p.note misses some elementsp.note matches only p elements with the class: a div with class="note" wasn't styledUse .note to match every element with the class
Nothing changesThe class name in the selector differs from the HTMLCompare the two letter by letter, case included

FAQ​

Can several elements share a class?​

Yes. The two notes in the example share note, and the one rule styled both.

How do I match an element that has two classes?​

Write both classes with no space between them. .note.urgent made only the paragraph with class="note urgent" bold. A paragraph with just class="urgent" stayed normal.

Conclusion​

Give the elements a class in the HTML, then select them with a dot and the class name. One class can style any number of elements, of any kind.

Additional Resources​