CSS .class Selector

Beginner
⏱️ 6 min read
📚 Updated: Jul 2026
🎯 4 Examples
Basic Selectors

What You’ll Learn

The .class selector is one of the most important tools in CSS. It lets you reuse styles across many elements by assigning the same class name in HTML.

01

.classname

Dot prefix.

02

Reusable

Many elements.

03

Multiple

Space in HTML.

04

element.class

Type + class.

05

Chained

.a.b together.

06

Naming

Clear labels.

Introduction

The .class selector in CSS targets HTML elements that have a specific class attribute value. Unlike styling a single element with an ID, classes are designed to be reused on many elements.

This makes classes essential for consistent design systems — buttons, cards, alerts, and layout utilities all rely on class-based styling.

Definition and Usage

In CSS, write a dot (.) followed by the class name with no space: .title or .btn-primary. In HTML, add class="title" to any element that should receive those styles.

💡
Beginner Tip

Class names describe what something is or how it looks — use names like card, alert-error, or btn-primary instead of vague names like red or big.

📝 Syntax

The signature of the class selector is:

syntax.css
.class {
  /* CSS properties */
}

Basic Example

class-basic.css
.title {
  color: #2563eb;
  font-size: 1.75rem;
}

.content {
  line-height: 1.6;
  color: #334155;
}

.button {
  background: #16a34a;
  color: #fff;
  padding: 0.5rem 1rem;
  border-radius: 6px;
}

Syntax Rules

  • Always start with a dot: .my-class (not my-class alone).
  • Class names are case-sensitive: .Card.card.
  • Hyphens are common: .btn-primary, .nav-link.
  • Multiple classes in HTML are space-separated: class="btn btn-primary".
  • Combine with element types: div.card for more specificity.

Related Selectors

  • #id — targets a single unique element by ID
  • element — type selector for all elements of that tag
  • .class1.class2 — element must have both classes
  • .class1 .class2 — descendant with class inside ancestor with class

⚡ Quick Reference

QuestionAnswer
Selector typeClass selector
CSS syntax.classname
HTML attributeclass="classname"
Reusable?Yes — on many elements
Multiple classesclass="a b" in HTML; .a.b in CSS
Browser supportAll browsers
.title .btn.btn-primary div.card .card.featured

When to Use .class

Class selectors are the default choice for most styling tasks:

  • Reusable components — Buttons, cards, badges, and alerts shared across pages.
  • Layout utilities — Classes like .container, .flex, or .text-center.
  • State modifiers — Add .active, .disabled, or .featured alongside a base class.
  • Typography — Shared heading and body text styles via .lead or .caption.
  • Framework patterns — Bootstrap, Tailwind, and most CSS frameworks are class-driven.

👀 Live Preview

Elements with class="cls-demo-title", class="cls-demo-content", and class="cls-demo-btn" receive styles from their matching class selectors:

Welcome to My Blog

Paragraphs with the same content class share identical styling.

Examples Gallery

Practice the .class selector with basic styling, multiple classes, type combinations, and chained selectors.

📜 Core Patterns

Apply styles to elements sharing the same class name.

Example 1 — Basic class styling

Style a heading, paragraphs, and button using separate class selectors.

class-styling.css
.title {
  color: #2563eb;
  font-size: 1.75rem;
}

.content {
  line-height: 1.6;
  color: #334155;
}

.button {
  background: #16a34a;
  color: #fff;
  padding: 0.5rem 1rem;
  border-radius: 6px;
}
Try It Yourself

How It Works

Every element with class="content" gets the same paragraph styles. You write the CSS once and reuse it anywhere in the HTML.

Example 2 — Multiple classes on one element

Stack a base class with modifiers: class="btn btn-primary btn-large".

multiple-classes.css
.btn {
  padding: 0.5rem 1rem;
  border: 1px solid #cbd5e1;
  border-radius: 6px;
  background: #f8fafc;
}

.btn-primary {
  background: #2563eb;
  border-color: #2563eb;
  color: #fff;
}

.btn-large {
  padding: 0.65rem 1.25rem;
  font-size: 1.05rem;
}
Try It Yourself

How It Works

The element inherits styles from every class it has. .btn sets the base look; .btn-primary and .btn-large add overrides on top.

📄 Specificity Patterns

Combine classes with element types and chain multiple class selectors.

Example 3 — element.class selector

Use div.card to style only <div> elements with the card class, not other tags.

element-class.css
div.card {
  background: #eff6ff;
  border: 1px solid #93c5fd;
  color: #1e40af;
  padding: 0.75rem 1rem;
  border-radius: 8px;
}

span.card {
  background: #fef3c7;
  border: 1px solid #fcd34d;
  color: #92400e;
  padding: 0.35rem 0.65rem;
  border-radius: 999px;
}
Try It Yourself

How It Works

Both elements share class="card", but div.card and span.card apply different styles based on the element type. This adds specificity without extra HTML classes.

Example 4 — Chained class selector (.class1.class2)

Match elements that have both classes using .card.featured with no space between selectors.

chained-class.css
.card {
  padding: 1rem 1.25rem;
  border: 1px solid #e2e8f0;
  border-radius: 10px;
  background: #fff;
}

.card.featured {
  border-color: #f59e0b;
  background: #fffbeb;
  box-shadow: 0 4px 14px rgba(245, 158, 11, 0.15);
}
Try It Yourself

How It Works

.card.featured requires class="card featured" on the same element. A card without featured only gets the base .card styles. Do not confuse this with .card .featured, which targets a descendant.

⚠️ Common Pitfalls

  • Vague names — Avoid .red or .big; use descriptive names like .alert-error or .text-lg.
  • Case sensitivity.Button and .button are different classes; keep naming consistent.
  • Missing dot in CSS — Write .title in CSS, not title (that would be an element selector).
  • Space vs no space.a.b means both classes on one element; .a .b means .b nested inside .a.
  • Overusing !important — Fix specificity with better selectors instead of forcing styles with !important.

♿ Accessibility

  • Classes are not semantics — A class="button" on a <div> is not a real button; use <button> for actions.
  • Do not rely on class alone — Screen readers ignore CSS classes; use proper HTML elements and ARIA when needed.
  • Visible focus — Style :focus on interactive elements, not just hover classes.
  • Color contrast — Ensure text classes like .text-muted still meet contrast guidelines.

🧠 How .class Works

1

Add class in HTML

You assign class="title" to one or more elements.

HTML
2

CSS defines .title

Your stylesheet declares rules for the .title selector.

CSS
3

Browser matches elements

Every element whose class list includes title gets the styles.

Match
=

Consistent reusable styling

One class definition styles many elements across the page.

Browser Compatibility

Class selectors are supported in every browser and have been a core part of CSS since CSS1.

Universal · All browsers

Class selectors everywhere

.class works in Chrome, Firefox, Safari, Edge, Opera, and all legacy browsers.

100% Browser support
Google Chrome All versions · Desktop & Mobile
Full support
Mozilla Firefox All versions · Desktop & Mobile
Full support
Apple Safari All versions · macOS & iOS
Full support
Microsoft Edge All versions
Full support
Opera All versions
Full support
.class selector 100% supported

Bottom line: Class selectors are the foundation of CSS styling and work everywhere without compatibility concerns.

Conclusion

The .class selector is the backbone of modern CSS. It enables reusable, maintainable styles across headings, paragraphs, buttons, cards, and entire design systems.

Master basic classes, multiple-class modifiers, element.class, and chained .class1.class2 patterns to write precise, readable stylesheets.

💡 Best Practices

✅ Do

  • Use meaningful, descriptive class names
  • Build base + modifier patterns (.btn + .btn-primary)
  • Keep class names lowercase with hyphens
  • Reuse classes for consistent design
  • Combine with semantic HTML elements

❌ Don’t

  • Use presentation-only names like .red-text
  • Mix up .a.b and .a .b
  • Rely on classes for semantics instead of HTML tags
  • Create a unique class for every single element
  • Forget that class names are case-sensitive

Key Takeaways

Knowledge Unlocked

Five things to remember about .class

Use these points when styling with classes.

5
Core concepts
🔁 02

Reusable

Many elements.

Purpose
03

Stack classes

Space in HTML.

Pattern
📄 04

element.class

More specific.

Combo
🌐 05

100% support

All browsers.

Compat

❓ Frequently Asked Questions

The .class selector targets every HTML element that has a matching class attribute value. It lets you apply the same styles to multiple elements across a page.
Use the class attribute: class="my-class". Multiple classes are separated by spaces: class="btn btn-primary".
Yes. .MyClass and .myclass are different selectors and match different class attribute values.
.class1.class2 (no space) matches one element that has both classes. .class1 .class2 (with a space) matches a .class2 element nested inside a .class1 ancestor.
Yes. Class selectors have been supported in every browser since the earliest days of CSS and are safe to use everywhere.

Practice in the Live Editor

Open the HTML editor and experiment with .title, .btn.btn-primary, and chained class selectors.

HTML Editor →

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.

5 people found this page helpful