CSS selectorSelectors

.class

Selects elements that have the given class in their class attribute. The most commonly used selector for styling components.

On this page

Example

#
CSS
.btn {
  padding: 8px 14px;
  border: 0;
  border-radius: 8px;
  background: #3f3f46;
  color: #fff;
  font-weight: 600;
}

.btn.primary {
  background: #7c3aed;
}
Preview

Definition and usage

#

A class selector starts with a dot: .card selects every element that has the word card in its class attribute. The attribute can hold several classes separated by spaces (class="card pro"), and the selector matches as long as the one you’re looking for is among them.

Classes are the foundation of solid CSS. You can give one class to many elements of different types, and a single element can carry several classes. A class’s specificity is (0, 1, 0): higher than a type selector, but much lower than an ID, so rules are easy to combine and override.

Several classes written together, with no space, mean "all of them at once": .btn.primary only hits an element that has both classes. With a space (.card .title), it’s an entirely different selector, the descendant combinator.

Case matters: .Card and .card are two different classes. A class name in a selector also can’t start with a digit (e.g. .1column) without escaping the character, so it’s simplest to avoid names like that.

Syntax

#
Syntax
.class-name { property: value; }

Notation variants

#
  • .tag

    Every element with the class tag, regardless of its tag or any other classes.
  • .tag.hot

    Two classes with no space: the element must have both at once.
  • button.tag

    A tag combined with a class: only buttons that have the class tag.
  • [class~="tag"]

    An attribute selector that works exactly like .tag and carries the same weight. A class is just a convenient shorthand.

Notation variants in practice

#

.tag

Every element with the class tag, regardless of its tag or any other classes.
.tag { background: #7c3aed; color: #fff; }

.tag.hot

Two classes with no space: the element must have both at once.
.tag.hot { background: #7c3aed; color: #fff; }

button.tag

A tag combined with a class: only buttons that have the class tag.
button.tag { background: #7c3aed; color: #fff; }

[class~="tag"]

An attribute selector that works exactly like .tag and carries the same weight. A class is just a convenient shorthand.
[class~="tag"] { background: #7c3aed; color: #fff; }

More examples

#
BEM naming: block, element, modifier
CSS
.card {
  padding: 12px 14px;
  border-radius: 12px;
  background: #18181b;
}

.card__title {
  margin: 0 0 4px;
  font-size: 16px;
}

.card--pro {
  outline: 2px solid #f59e0b;
}
Preview

In BEM, every part of a component gets its own class, so all the selectors carry the same low weight and don’t depend on the HTML structure.

Browser support

#

Widely available

Works in all modern browsers, phones included. You can use it without worry.

  • Chrome
  • Edge
  • Firefox
  • Safari

Good practices

#
  • Name classes after their role, not their appearance: .alert survives a color change, .red-text doesn’t.
  • Instead of lengthening the selector (.sidebar .menu .item), give the element its own class. The rules stay shorter, and the specificity stays low and predictable.
  • In JavaScript you add and remove classes with element.classList.add("active") or classList.toggle(). It’s the simplest way to change the look of something in response to user action.

Related entries

#

Spotted a mistake or missing an example? Write to us.