CSS Class Selector: Single, Multiple and Combined Classes
Contents
A class selector targets every element with a given class. In HTML you add the class with the class attribute; in CSS you select it with a dot followed by the class name:
<p class="intro"> is styled by .intro { ... }
Classes are the workhorse of CSS: reusable, easy to override, and the right default for almost all styling.
The basics
Any number of elements can share a class, and the rule styles all of them.
<p class="intro">First paragraph</p>
<p class="intro">Second paragraph</p>
<style>
.intro {
font-size: 1.25rem;
color: #475569;
}
</style>
Multiple classes on one element
Separate class names with spaces in the class attribute. The element gets the styles of every class it has. This is how you combine a base style with variations, like in BEM or Tailwind.
<button class="btn btn-primary btn-large">Save</button>
<style>
.btn { padding: 8px 16px; border-radius: 8px; }
.btn-primary { background: #1d4ed8; color: #fff; }
.btn-large { font-size: 1.25rem; }
</style>
Combining class selectors
Spaces and commas completely change what a selector means. These three look alike but select different things:
Demo:
.cls-a.cls-b: both classes on one element
.cls-a .cls-b: .cls-b inside .cls-a
.cls-c, .cls-d: either class
.cls-c, .cls-d: either class
.a.b: an element with both classes
No space between the selectors means “the same element must have both”. Handy for state classes like .tab.active.
.tab.active {
border-bottom: 2px solid #1d4ed8;
}
.a .b: descendant
A space means “a .b somewhere inside a .a“, at any depth. Use > for direct children only: .menu > .item.
.card .title { font-weight: 700; } /* any .title inside .card */
.menu > .item { padding: 8px; } /* only direct children */
.a, .b: a selector list
A comma applies the same declarations to several selectors. It’s just shorthand for writing the rule twice.
.alert,
.notice {
padding: 16px;
border-radius: 8px;
}
Element with a class: p.intro
Put a tag name in front to only match that type of element. Usually unnecessary; it raises specificity and makes the class less reusable.
p.intro { font-size: 1.25rem; } /* only <p class="intro"> */
Class vs id
Class (.name) |
Id (#name) |
|
|---|---|---|
| Per page | Any number of elements | Must be unique |
| Per element | Many classes | One id |
| Specificity | Low (0,1,0), easy to override | High (1,0,0), hard to override |
| Use for | Styling | Anchor links, form labels, JavaScript hooks |
Style with classes. An id in a stylesheet beats any number of classes, which is a common reason people end up reaching for !important.
Class naming rules
- Class names are case-sensitive:
.Introdoesn’t matchclass="intro". - Use letters, digits, hyphens and underscores. A name can’t start with a digit or a hyphen followed by a digit in CSS without escaping.
- Name by purpose (
.error), not appearance (.red-text), so the name still makes sense after a redesign.
<div class="2col">...</div>
<style>
.2col { } /* invalid: starts with a digit */
.\32 col { } /* valid but ugly: escaped "2" */
.two-col { } /* better: rename the class */
</style>
Add, remove and toggle classes with JavaScript
classList changes classes without touching the others. Toggling a class and styling it in CSS is the cleanest way to change an element’s look from JavaScript.
const menu = document.querySelector('.menu');
menu.classList.add('is-open');
menu.classList.remove('is-open');
menu.classList.toggle('is-open'); // returns true if it was added
menu.classList.contains('is-open'); // true / false
document.querySelectorAll('.card.featured'); // select by several classes
FAQ
How do I add two classes to one element?
Separate them with a space: class="btn primary". The element is styled by both .btn and .primary.
How do I select an element with two classes?
Write the class selectors without a space: .btn.primary.
What’s the difference between .a.b and .a .b?
.a.b is one element with both classes. .a .b is a .b element somewhere inside a .a element.
If two classes set the same property, which one wins?
The rule that comes last in the CSS wins, because both have the same specificity. The order of the classes in the HTML attribute doesn’t matter.
Can a class name have spaces?
No. A space separates two class names, so class="main title" is two classes.
Related: BEM naming, CSS !important, ::before and ::after and the CSS tutorial.