# CSS Selectors and Specificity

> How a CSS rule picks the parts of a page it styles. Why one rule beats another, and how the specificity score breaks the tie.

**Course:** [HTML, CSS and JavaScript](https://learn.benibix.com/learn/development-web)  
**Time:** 13 min

**After this lesson you can:**

- Write a CSS rule with a selector
- Use a pseudo-class such as :hover
- Work out which of two rules wins

## How CSS picks an element, and which rule wins

CSS is the language that sets how a web page looks. A CSS rule has two parts. The selector says which elements to style, and the declarations say how, such as color: red. When two rules clash, the browser scores each selector. An id scores 1-0-0, a class 0-1-0, and a tag 0-0-1.

There are three basic selectors. A tag name, such as p, picks every paragraph. A class, written with a dot as .price, picks every element with class="price". An id, written with a hash as #cart, picks the one element with id="cart". Classes are the ones you will use most.

Selectors can be joined. Writing nav a picks only the links inside a nav. Writing .card.sale picks elements that have both classes. A comma lists several selectors that share one set of declarations, so h1, h2 styles both kinds of heading at once.

A pseudo-class picks an element by its state, not its name. It starts with a colon. The rule a:hover styles a link while the mouse is over it. The rule input:focus styles a box while you type in it. And li:first-child picks the first item in a list.

When two rules set the same thing on one element, the browser has to pick. It compares the scores column by column, from the left. The first column that differs decides it. So one id at 1-0-0 beats ten classes at 0-10-0, because the id column is compared first.

If the scores are equal, the rule that comes later in the file wins. A style put in the tag's own style attribute beats them all. Adding !important to a line jumps it above the normal rules. That is why it causes so much trouble later.

People often reach for !important or an id when a style will not apply. It works once, and then the next fix needs a stronger rule still. Keep most selectors to one class. Then scores stay low and equal, and the order in the file decides.

```css
/* 0-0-1: every paragraph */
p { color: #333; }

/* 0-1-0: anything with class="price" */
.price { color: green; font-weight: bold; }

/* 0-1-1: a link while the mouse is over it */
a:hover { text-decoration: underline; }

/* 1-0-0: beats both rules above on the same element */
#cart { color: red; }
```
Each comment shows the selector's score. The higher score wins on any element both rules hit.

![A table with three columns, ID, CLASS and TYPE, and four selectors. p scores 0-0-1, .note scores 0-1-0, a:hover scores 0-1-1, and #intro .note p scores 1-1-1 and is marked the winner.](https://learn.benibix.com/diagrams/dev5-specificity.svg)

### Example: Jack's button will not turn blue

Jack writes .btn { color: blue; } but his button stays grey. An older rule, #header .btn, scores 1-1-0 and beats his 0-1-0.

Rather than add !important, he removes the id from the old rule. Both rules now score 0-1-0, so his later one wins, and the button turns blue.

**Words in this lesson:** [Cascading Style Sheets (CSS)](https://learn.benibix.com/term/development/css), [CSS Selector](https://learn.benibix.com/term/development/css-selector), [CSS Pseudo-Class](https://learn.benibix.com/term/development/css-pseudo-class), [CSS Specificity](https://learn.benibix.com/term/development/css-specificity)

**Question:** Which CSS selector wins: one id, or ten classes?

**Answer:** The id, because the id column is compared first. Scores are compared from the left column. One id is 1-0-0 and ten classes are 0-10-0, so the id wins before the class column is even read.

---

Source: https://learn.benibix.com/learn/development-web/css-selectors-and-specificity
Author: Behnia Zarei (a developer in Vienna, Austria, who builds and teaches data, AI and business intelligence at BENIBIX)
Publisher: BENIBIX Learn (https://learn.benibix.com/)
Last updated: 2026-11-19
Licence: CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Quote freely with attribution.
