Skip to content
BENIBIX Learn

CSS Selectors and Specificity

Level 3 · CSS basics

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 contentsLesson 7 of 14

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

13 mincovers 4 glossary entries

Worth reading first: HTML Elements and Attributes

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.
The columns are compared from the left. One id beats any number of classes.

Worked 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.

The words for this lesson4

Check yourself

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

Finished this lesson?

Marking it complete also marks its 4 glossary entries learned, which updates your roadmap and your saved page. 0 are already marked.