CSS Class Selector (.class)

Beginner
5 min read
Updated: Oct 2026
3 examples
Live preview

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.

CSS Syntax

syntax.css
.class_name { /* styles */ }

The name must be a valid CSS identifier. Unusual characters in HTML class values need escaping in the selector.

Common Patterns

PatternDescriptionTry it
.noteAny element with that classTry it
Multiple classes in HTMLclass="red yellow-bg" applies both rulesTry it
.a.bOne element that has both classesTry it
p.noteOnly p elements with that classTry it
.a .bDescendant: .b inside .aTry it
Escaped nameClass values that need CSS escapingTry it

How to Use Class Selectors

Name the class in HTML, then target it with a dotted selector in CSS.

  1. Add class="name" (or several names) on the element(s).
  2. Write .name { … } in your stylesheet.
  3. Reuse the same class wherever that look should apply.
  4. Chain .a.b only when one element must have both classes.

Live Preview

Toggle classes on the sample paragraph. Watch how .red, .yellow-bg, and .fancy stack.

Classes

HTML becomes class="…" with the active names. Order in the attribute does not change which rules apply.

Sample

Styled with class selectors.

class="red"

CSS Examples

Each snippet focuses on .class. Use Try It Yourself for a live editor.

Example 1 — Basic .note

Style every element that has the note class.

CSS
<style>
  .note {
    color: #b91c1c;
    font-weight: 700;
  }
</style>

<p class="note">Important tip.</p>
<p>Regular paragraph.</p>
Try It Yourself

How It Works

  1. HTML sets the hook. class="note" marks the first paragraph.
  2. CSS uses a dot. .note means “elements with that class.”
  3. Only matches apply. The second paragraph has no class, so it stays plain.
  4. Reusable. Add class="note" to any tag to reuse the style.

Example 2 — Multiple Classes

One element can take several class styles at once.

CSS
<style>
  .red { color: #dc2626; }
  .yellow-bg { background: #fef08a; }
</style>

<p class="red yellow-bg">Red text on yellow.</p>
Try It Yourself

How It Works

  1. Two class tokens. Space-separated names in HTML.
  2. Two separate rules. Each .class contributes its own properties.
  3. Styles combine. Color and background both apply.
  4. 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).

CSS
<style>
  .alert {
    padding: 0.5rem;
    background: #fee2e2;
  }
  .alert.ok {
    background: #dcfce7;
  }
</style>

<p class="alert">Warning</p>
<p class="alert ok">Success</p>
Try It Yourself

How It Works

  1. .alert alone. Gives the red-tint base.
  2. .alert.ok (no space). Matches only when both classes are present.
  3. Second paragraph matches. It has alert and ok.
  4. 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 .b

No 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.

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.

100% Browser support
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.

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

It matches every element whose class attribute includes that class name, so you can reuse the same styles in many places.
Use class="name". Separate multiple classes with spaces: class="btn primary".
Yes. .Card and .card are different and match different attribute values.
.a.b (no space) matches one element that has both classes. .a .b (space) matches a .b element nested inside an .a ancestor.
Yes for a single class token. The attribute form [class~="name"] is equivalent to .name.
They are Baseline and widely available. Supported in every modern browser and classic IE. This is browser CSS, not a Node.js API.

Next: element.class

Combine a tag name with a class for tighter matching.

element.class tutorial →

About the author

Mari Selvan M P
Mari Selvan M P 🔗

Developer, cloud engineer, and technical writer

  • Experience 12 years building web and cloud systems
  • Focus Full Stack Development, AWS, and Developer Education

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