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.
Fundamentals
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.
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.
.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.
Watch Out
⚠️ 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.
A11y
♿ 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.
Compatibility
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 ChromeAll versions · Desktop & Mobile
Full support
Mozilla FirefoxAll versions · Desktop & Mobile
Full support
Apple SafariAll versions · macOS & iOS
Full support
Microsoft EdgeAll versions
Full support
OperaAll versions
Full support
.class selector100% supported
Bottom line: Class selectors are the foundation of CSS styling and work everywhere without compatibility concerns.
Wrap Up
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.
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
Summary
Key Takeaways
Knowledge Unlocked
Five things to remember about .class
Use these points when styling with classes.
5
Core concepts
●01
Dot prefix
.name in CSS.
Syntax
🔁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.