Beginner · CSS basics · topic 15 of 64 · 30 min
Selectors, specificity and the cascade
Explanation
The common selectors
| Selector | Picks | Example |
|---|---|---|
| p | Every <p> (type selector) | p { line-height: 1.6; } |
| .price | Every element with class="price" | .price { color: green; } |
| #cart | The one element with id="cart" | #cart { position: sticky; } |
| [type="email"] | Elements with that attribute value | input[type="email"] { width: 100%; } |
| h1, h2 | Both (a group) | h1, h2 { font-family: serif; } |
| nav a | Any <a> inside <nav> (descendant) | nav a { text-decoration: none; } |
| ul > li | Only <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.
.btn:hover { background: #1e40af; }
.btn:focus-visible { outline: 3px solid #f59e0b; }
tr:nth-child(even) { background: #f3f4f6; }
.required::after { content: " *"; color: #b91c1c; }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).
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).
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 */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.
Watch the lesson
Selectors, specificity and the cascade · 1/6
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)
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.
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
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.