Selectors, classes and the cascade
Style exactly the elements you want with type, class, descendant, grouped and hover selectors, and learn how the cascade and specificity decide which CSS rule wins.
🎯 By the end of this lesson you will be able to:
- Use type, class, descendant and grouped selectors
- Style links for hover and keyboard focus
- Predict which rule wins using specificity and source order
Outline
What you'll learn and why it matters
So far each rule has styled every element of one type, such as every <p>. Real designs need more control: *this* paragraph is an introduction, *these* spans are prices, *that* price is on sale. Selectors give you that control. And when two rules disagree, the cascade decides who wins: understanding it saves hours of "why is my CSS not working?".
Classes: your own names for elements
Add a class attribute in the HTML, then select it in CSS with a dot in front of the name. Many elements can share a class, and one element can have several classes separated by spaces.
<p class="intro">Everything is made fresh every morning.</p>
<span class="price sale">₹60</span>.intro {
font-size: 1.25rem;
font-style: italic;
}
.price {
font-weight: 700;
color: #166534;
}
.sale {
background-color: #fef08a;
}Show the HTML this CSS styles
<h1>Menu</h1>
<p class="intro">Everything is made fresh every morning.</p>
<ul>
<li>Masala chai <span class="price">₹40</span></li>
<li>Filter coffee <span class="price">₹50</span></li>
<li>Banana bread <span class="price sale">₹60</span></li>
</ul>
<p>Ask about today's specials.</p>Browser preview
The banana bread price has both classes, so it gets the bold green style from .price and the yellow background from .sale.
More ways to select
| Selector | Example | Selects |
|---|---|---|
| Type | p | Every <p> element |
| Class | .price | Every element with class="price" |
| ID | #opening-hours | The one element with id="opening-hours" |
| Descendant (space) | li .price | Elements with class price that are anywhere inside an <li> |
| Group (comma) | h1, h2 | Every h1 and every h2: same rule, written once |
| Universal | * | Every element |
h1, h2 {
font-family: Georgia, serif;
color: #7c2d12;
}
ul .price {
float: right;
}
li {
max-width: 20rem;
}Show the HTML this CSS styles
<h1>Menu</h1>
<p class="intro">Everything is made fresh every morning.</p>
<ul>
<li>Masala chai <span class="price">₹40</span></li>
<li>Filter coffee <span class="price">₹50</span></li>
<li>Banana bread <span class="price sale">₹60</span></li>
</ul>
<p>Ask about today's specials.</p>Browser preview
ul .price only selects prices inside a list, so a price elsewhere on the page would keep its normal place.
Hover and focus: styling links
A pseudo-class selects an element in a particular state. It starts with a colon. The most useful ones are for links:
a {
color: #1d4ed8;
}
a:hover {
color: #1e3a8a;
text-decoration-thickness: 3px;
}
a:focus-visible {
outline: 3px solid #f59e0b;
outline-offset: 2px;
}Show the HTML this CSS styles
<p>Read <a href="menu.html">our full menu</a> or <a href="contact.html">contact us</a>.</p>
<p>Press the Tab key with the preview selected to see the focus outline.</p>Browser preview
| Pseudo-class | When it applies |
|---|---|
:hover | The mouse pointer is over the element |
:focus-visible | The element is focused from the keyboard (Tab key) |
:visited | A link the visitor has already opened |
:first-child | The element is the first child of its parent |
The cascade: which rule wins?
When two rules set the same property on the same element, the browser picks one using these steps:
| Step | Rule | Example |
|---|---|---|
| 1. Specificity | The more specific selector wins: ID beats class, class beats type | .intro beats p |
| 2. Source order | If specificity is equal, the rule written later wins | Two p rules: the second one wins |
p {
color: gray;
}
.note {
color: green;
}
p {
color: black;
}Show the HTML this CSS styles
<p class="note">This paragraph has the class note.</p>
<p>This one has no class.</p>Browser preview
The first paragraph is green: .note (a class) is more specific than p, so it wins even though a p rule comes later. The second paragraph is black: only the two p rules match it, they have equal specificity, so the later one wins.
Practice
Guided exercise
Highlight the important things
In the notice board below, make the .urgent notice bold with a light red background, make every .date grey and smaller, and make links in the notices underline thickly on hover.
- Write a
.urgentrule withfont-weightandbackground-color. - Write a
.daterule withcolorand a smallerfont-sizesuch as0.875rem. - Write
a:hoverwithtext-decoration-thickness.
Show the solution
.urgent {
font-weight: 700;
background-color: #fee2e2;
}
.date {
color: #4b5563;
font-size: 0.875rem;
}
a:hover {
text-decoration-thickness: 3px;
}Show the HTML this CSS styles
<h1>Notice board</h1>
<p class="urgent">The library is closed tomorrow. <span class="date">12 March</span></p>
<p>New books have arrived. <span class="date">10 March</span> <a href="books.html">See the list</a></p>Browser preview
How to get there: Each class selects only the elements that carry it, so the urgent notice gets a background but the other notice does not. The date spans share one rule because they share a class.
Your turn
Predict the winner
Without running it, decide the colour of each paragraph. Then check with the preview.
Rules in order: .tip { color: green; }, p { color: red; }, .tip { color: blue; }.
HTML: <p class="tip">One</p> and <p>Two</p>.
Show the solution
.tip {
color: green;
}
p {
color: red;
}
.tip {
color: blue;
}Show the HTML this CSS styles
<p class="tip">One</p>
<p>Two</p>Browser preview
How to get there: One is blue: both .tip rules beat p on specificity, and between the two .tip rules the later one wins. Two is red: only the p rule matches it.
Common mistakes
Forgetting the dot before a class name
You see: price { … } looks for a <price> element and matches nothing
Fix: Class selectors start with a dot: .price.
Putting the dot in the HTML
You see: class=".price" does not match .price
Fix: Write class="price" in HTML and .price in CSS.
Adding a space in p.note
You see: p .note means *a .note inside a p*, which is different
Fix: p.note (no space) means a p that has the class note.
Fighting specificity with !important
You see: Styles become impossible to override later
Fix: Use a class selector, or place your rule later in the file.
Removing focus outlines
You see: Keyboard users cannot see where they are
Fix: Style :focus-visible with a clear outline instead.
Recap
- Classes (
.name) let you style chosen elements; an element can have several classes. - Descendant (
nav a) and group (h1, h2) selectors target and combine rules. :hoverand:focus-visiblestyle states; always keep a visible focus style.- When rules clash, higher specificity wins; if equal, the later rule wins.
Key terms
- Class
- A reusable name added to elements with class="…" and selected in CSS with a dot.
- Pseudo-class
- A selector for a state, such as :hover or :focus-visible.
- Specificity
- How specific a selector is; the more specific rule wins a conflict.
- Cascade
- The process the browser uses to decide which rule applies when several match.
Check your understanding
Answer all 4 questions, then check your answers to see an explanation for each one.
1. Which selector styles every element with class="card"?
2. `p { color: red; }` comes after `.note { color: blue; }`. What colour is `<p class="note">`?
3. What does the selector `nav a` select?
4. Why should you style :focus-visible rather than remove outlines?
Mark this lesson complete
Finished this lesson?
When you have worked through the examples and exercises, mark it complete to save your progress.