# CSS Class Selector: Single, Multiple and Combined Classes

> How CSS class selectors work: adding multiple classes to an element, .a.b vs .a .b vs .a, .b, class vs id, naming rules, specificity, and adding or removing classes with JavaScript.

Author: [Marko Denic](https://markodenic.com/) | Published: 2026-10-21 | Updated: 2026-10-21 | Topics: CSS, HTML, Tutorial | Canonical: https://markodenic.com/css-class-selector/

![CSS Class Selector: Single, Multiple and Combined Classes](https://markodenic.com/wp-content/uploads/2026/10/og-css-class-selector.png)

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.

```html
<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](https://markodenic.com/bem-css/) or Tailwind.

```html
<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:

```html
<div class="cls-demo">
  <p class="cls-a cls-b">.cls-a.cls-b: both classes on one element</p>
  <div class="cls-a">
    <p class="cls-b">.cls-a .cls-b: .cls-b inside .cls-a</p>
  </div>
  <p class="cls-c">.cls-c, .cls-d: either class</p>
  <p class="cls-d">.cls-c, .cls-d: either class</p>
</div>
```

```css
.cls-demo {
  display: grid;
  gap: 10px;
  padding: 20px;
  border-radius: 16px;
  background: #f8fafc;
  font: 600 15px/1.4 ui-monospace, monospace;
}

.cls-demo p {
  margin: 0;
  padding: 10px 14px;
  border-radius: 8px;
  background: #e2e8f0;
  color: #0f172a;
}

/* No space: the element has BOTH classes */
.cls-a.cls-b { background: #1d4ed8; color: #fff; }

/* Space: a .cls-b INSIDE a .cls-a */
.cls-a .cls-b { background: #0e7490; color: #fff; }

/* Comma: EITHER class, same styles */
.cls-c,
.cls-d { background: #e0e7ff; color: #1e3a8a; }
```

[See the live demo](https://markodenic.com/css-class-selector/#demo)

### .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`.

```css
.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`.

```css
.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.

```css
.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.

```css
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](https://markodenic.com/css-important/).

### Class naming rules

- Class names are **case-sensitive**: `.Intro` doesn’t match `class="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.

```html
<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. For example, add an `.active` class to the current nav link and give it an [underline](https://markodenic.com/css-underline/). To grab the element first, pass the same selector to [querySelector()](https://markodenic.com/queryselector-vs-queryselectorall/): `document.querySelector('.tab.active')`.

```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](https://markodenic.com/bem-css/), [CSS !important](https://markodenic.com/css-important/), [::before and ::after](https://markodenic.com/css-before-after/) and the [CSS tutorial](https://markodenic.com/css-tutorial/).
