Skip to content

Beginner · CSS basics · topic 15 of 64 · 30 min

Selectors, specificity and the cascade

Explanation

Selectors choose which elements a rule styles: by tag, class, id, attribute or position. When two rules disagree, the browser picks the winner using the cascade: more specific selectors win, and among equals, the later rule wins. Classes are the everyday tool.

The common selectors

SelectorPicksExample
pEvery <p> (type selector)p { line-height: 1.6; }
.priceEvery element with class="price".price { color: green; }
#cartThe one element with id="cart"#cart { position: sticky; }
[type="email"]Elements with that attribute valueinput[type="email"] { width: 100%; }
h1, h2Both (a group)h1, h2 { font-family: serif; }
nav aAny <a> inside <nav> (descendant)nav a { text-decoration: none; }
ul > liOnly <li> directly inside <ul> (child)ul > li { margin: 4px; }
*Every element* { box-sizing: border-box; }

Pseudo-classes and pseudo-elements

A pseudo-class selects an element in a certain state or position: a:hover (mouse over it), button:focus-visible (keyboard focus), li:first-child, tr:nth-child(even) for striped tables. A pseudo-element styles a part of an element: p::first-line, input::placeholder, or ::before to add decoration.

css
.btn:hover { background: #1e40af; }
.btn:focus-visible { outline: 3px solid #f59e0b; }
tr:nth-child(even) { background: #f3f4f6; }
.required::after { content: " *"; color: #b91c1c; }
States, positions and an added star after required labels

The cascade: who wins?

Often two rules set the same property on the same element. The cascade is the set of rules the browser uses to choose. In simple terms: first importance (!important), then specificity, then order (the later rule wins).

Think of it like this: Think of instructions at a wedding. The caterer has a general rule ("serve all guests tea"), table 5 has a special rule ("serve table 5 coffee") and one guest has a personal request ("no sugar for Mr Rao"). The most specific instruction wins for each person.

Specificity in simple counting

Count the selector as three numbers: ids, then classes/attributes/pseudo-classes, then types/pseudo-elements. Compare from the left. #cart (1,0,0) beats .menu .item (0,2,0), which beats nav ul li (0,0,3).

css
p { color: black; }            /* 0,0,1 */
.note { color: blue; }         /* 0,1,0  wins over p */
#intro { color: green; }       /* 1,0,0  wins over .note */
.note { color: purple; }       /* same as first .note, but later: wins over it */
A paragraph with id="intro" and class="note" turns green

Inheritance

Some properties, mostly about text (color, font-family, font-size, line-height), pass down from a parent to its children. That is inheritance. Set font-family once on body and the whole page uses it. Box properties such as margin, border and padding do not inherit.

Tip: Style with classes most of the time. They are reusable, easy to read and keep specificity low and predictable. Save ids for links (#section) and JavaScript.
Careful: Avoid !important. It wins over almost everything, so the next person must use another !important to change it, and the CSS becomes a fight. Fix the selector instead.

Watch the lesson

Selectors, specificity and the cascade · 1/6

Selector = who gets styled

Selectors decide which elements a rule styles.

Prefer a video by a teacher? Find free videos on “css selectors specificity cascade explained” on YouTube ↗

In real life: Highlighting the cheapest plan

A mobile recharge site shows three plan cards with class="plan". The best-value card also has class="plan featured". The CSS .plan sets a white background, and .plan.featured (two classes, more specific) sets a yellow border and a bigger shadow, so only that card stands out without any extra HTML.

Practice (0/2 done)

Task 1 · Code · HTML

Write CSS so that elements with class "price" are coloured green, and every even row of the table (tr:nth-child(even)) gets the background colour #eeeeee.

Task 2 · Code · Javascript

Write a function specificity(selector) for simple selectors made of one part, like "p", ".note" or "#intro". Return an array [ids, classes, types]: "#intro" gives [1, 0, 0], ".note" gives [0, 1, 0], and "p" gives [0, 0, 1].

Quick quiz

Question 1

1.Which selector picks every element with class="card"?

Question 2

2.Which rule wins for <p id="a" class="b">: #a { color: red } or .b { color: blue }?

Question 3

3.Two rules have the same specificity. Which wins?

Question 4

4.Which property is inherited by child elements?

Question 5

5.What does "nav a" select?

Read more (free)

Pass the quiz (60% or more) or mark this topic as done when you are ready.

Your progress is saved on this device only.

← Previous: How CSS works