How to Select Elements by Class in CSS
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
.<class name> {
<declarations>
}
| Part | What 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:

<!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 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
| Mistake | What happens | Do this instead |
|---|---|---|
Leaving out the dot: note | Without the dot, it's a selector for elements named note, so nothing changed | Start the selector with a dot |
Changing the case: .Note for class="note" | .Note matched nothing, because the case must match | Copy the class name exactly |
Troubleshooting
| Symptom | Likely cause | Fix |
|---|---|---|
A rule such as p.note misses some elements | p.note matches only p elements with the class: a div with class="note" wasn't styled | Use .note to match every element with the class |
| Nothing changes | The class name in the selector differs from the HTML | Compare 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
- Class selectors, on MDN.
