← Cheat Sheets

CSS Specificity and the Cascade

Why one rule beats another.

# CSS Specificity and the Cascade

Why one rule beats another.

Specificity is compared left to right as (inline, IDs, classes, elements):

| Selector part | Weight |
|---|---|
| Inline style | highest (1,0,0,0) |
| `#id` | 0,1,0,0 |
| `.class`, `[attr]`, `:pseudo-class` | 0,0,1,0 |
| `element`, `::pseudo-element` | 0,0,0,1 |
| `*`, `:where()` | 0 |

If specificity ties, the **later rule wins**. `!important` overrides normal rules; avoid it. Prefer low-specificity class selectors and order rules deliberately.