Home CSS Selectors .class CSS Class Selector (.class) Definition
Definition and Usage The class selector matches elements whose class attribute includes a given name. In CSS you write a dot, then the name: .spacious.
One class can style many elements. One element can have many classes (space-separated in HTML). Prefer clear, reusable names for components and states.
Baseline · Widely available. Not deprecated. Supported since the earliest CSS browsers; Baseline dating cites July 2015 for the modern web platform set.
Equivalent attribute form. .name matches the same elements as [class~="name"] for a single class token.
Syntax
CSS Syntax .class_name { /* styles */ }
The name must be a valid CSS identifier. Unusual characters in HTML class values need escaping in the selector.
Patterns
Common Patterns Pattern Description Try it .noteAny element with that class Try itMultiple classes in HTML class="red yellow-bg" applies both rules Try it.a.bOne element that has both classes Try itp.noteOnly p elements with that class Try it.a .bDescendant: .b inside .a Try itEscaped name Class values that need CSS escaping Try it
Approach
How to Use Class Selectors Name the class in HTML, then target it with a dotted selector in CSS.
Add class="name" (or several names) on the element(s). Write .name { … } in your stylesheet. Reuse the same class wherever that look should apply. Chain .a.b only when one element must have both classes. Try it
Live Preview Toggle classes on the sample paragraph. Watch how .red, .yellow-bg, and .fancy stack.
Code
CSS Examples Each snippet focuses on .class. Use Try It Yourself for a live editor.
1. Basic .class 2. Multiple classes 3. Chained .a.b Example 1 — Basic .note Style every element that has the note class.
<style>
.note {
color: #b91c1c;
font-weight: 700;
}
</style>
<p class="note">Important tip.</p>
<p>Regular paragraph.</p> Important tip.
Regular paragraph.
How It Works HTML sets the hook. class="note" marks the first paragraph.CSS uses a dot. .note means “elements with that class.”Only matches apply. The second paragraph has no class, so it stays plain.Reusable. Add class="note" to any tag to reuse the style.Example 2 — Multiple Classes One element can take several class styles at once.
<style>
.red { color: #dc2626; }
.yellow-bg { background: #fef08a; }
</style>
<p class="red yellow-bg">Red text on yellow.</p> How It Works Two class tokens. Space-separated names in HTML.Two separate rules. Each .class contributes its own properties.Styles combine. Color and background both apply.Mix and match. Other elements can use just red or just yellow-bg.Example 3 — Chained .alert.ok Require both classes on the same element (no space between dots).
<style>
.alert {
padding: 0.5rem;
background: #fee2e2;
}
.alert.ok {
background: #dcfce7;
}
</style>
<p class="alert">Warning</p>
<p class="alert ok">Success</p> How It Works .alert alone. Gives the red-tint base..alert.ok (no space). Matches only when both classes are present.Second paragraph matches. It has alert and ok.Not a descendant. .alert .ok (with a space) would mean nesting instead. Edge Cases & Pitfalls Most class bugs are typos, case mismatches, or a missing/extra space between selectors.
case Case must match .Btn will not style class="btn". Keep HTML and CSS spelling identical.
space .a.b vs .a .bNo space = both classes on one element. A space = descendant combinator.
identifiers Escape odd class names Names starting with a digit or containing ? need escaping (for example .\31 23item). Prefer simple letters, digits, hyphens.
vs-id Class vs id Classes are reusable. An #id should be unique on the page and is more specific.
element Limit by tag when needed Use p.note so only paragraphs with that class match, not every tag.
Compatibility
Browser Support The class selector (.class) is a browser CSS selector . Node.js, Deno, and Bun do not apply it.
✓ Baseline · Widely available
.class Supported in all browsers, including classic Internet Explorer. Core CSS since CSS1.
Google Chrome 1+
Full support
Mozilla Firefox 1+
Full support
Apple Safari 1+
Full support
Microsoft Edge 12+
Full support
Opera 3.5+
Full support
Samsung Internet 1+
Full support
Internet Explorer 3+
Full support .class Universally supported
Bottom line: Safe everywhere. Prefer readable class names over escaped oddities. It is not a runtime API.
Remember
Key Takeaways Role: reuse styles with .name matching class="name".
Many-to-many: one class on many elements; many classes on one element.
Chain carefully: .a.b means both on one node.
Names: keep them simple, lowercase, and consistent.
One line: use .class to apply the same styles wherever you add that class in HTML.
Frequently Asked Questions What does the CSS class selector do? It matches every element whose class attribute includes that class name, so you can reuse the same styles in many places.
How do I add a class in HTML? Use class="name". Separate multiple classes with spaces: class="btn primary".
Are class names case-sensitive? Yes. .Card and .card are different and match different attribute values.
What is .a.b versus .a .b? .a.b (no space) matches one element that has both classes. .a .b (space) matches a .b element nested inside an .a ancestor.
Is .class the same as [class~="name"]? Yes for a single class token. The attribute form [class~="name"] is equivalent to .name.
Which browsers support class selectors? They are Baseline and widely available. Supported in every modern browser and classic IE. This is browser CSS, not a Node.js API.
About the author Developer, cloud engineer, and technical writer
I write practical tutorials so students and working developers can learn by doing—from databases and APIs to deployment on AWS.
8 people found this page helpful
Helpful Share Copy link Suggestion