Nishaglobal Education logo

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.

Lesson 7 of 1335 minQuiz: 4 questions

🎯 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
  1. What you'll learn and why it matters
  2. Classes: your own names for elements
  3. More ways to select
  4. Hover and focus: styling links
  5. The cascade: which rule wins?
  6. Practice
  7. Common mistakes
  8. Recap
  9. Quiz
  10. Mark lesson complete

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.

Classes in HTML
<p class="intro">Everything is made fresh every morning.</p>
<span class="price sale">₹60</span>
CSS
.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

SelectorExampleSelects
TypepEvery <p> element
Class.priceEvery element with class="price"
ID#opening-hoursThe one element with id="opening-hours"
Descendant (space)li .priceElements with class price that are anywhere inside an <li>
Group (comma)h1, h2Every h1 and every h2: same rule, written once
Universal*Every element
CSS
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:

CSS
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-classWhen it applies
:hoverThe mouse pointer is over the element
:focus-visibleThe element is focused from the keyboard (Tab key)
:visitedA link the visitor has already opened
:first-childThe 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:

StepRuleExample
1. SpecificityThe more specific selector wins: ID beats class, class beats type.intro beats p
2. Source orderIf specificity is equal, the rule written later winsTwo p rules: the second one wins
Three rules match the same paragraph:<p class="note" id="intro">p { color: gray; }type selector · specificity 0 · 0 · 1loses.note { color: green; }class selector · specificity 0 · 1 · 0loses#intro { color: purple; }id selector · specificity 1 · 0 · 0✓ winsMore specific selector wins: id beats class beats type.Equal specificity: the rule written last wins.
Three rules colour the same paragraph: p sets gray with specificity 0-0-1, .note sets green with 0-1-0, and #intro sets purple with 1-0-0. The id selector is most specific, so the text is purple. With equal specificity, the later rule wins.
CSS
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.

  1. Write a .urgent rule with font-weight and background-color.
  2. Write a .date rule with color and a smaller font-size such as 0.875rem.
  3. Write a:hover with text-decoration-thickness.
Show the solution
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
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.
  • :hover and :focus-visible style 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. 1. Which selector styles every element with class="card"?

  2. 2. `p { color: red; }` comes after `.note { color: blue; }`. What colour is `<p class="note">`?

  3. 3. What does the selector `nav a` select?

  4. 4. Why should you style :focus-visible rather than remove outlines?

Answer all 4 questions to check.

Mark this lesson complete

Finished this lesson?

When you have worked through the examples and exercises, mark it complete to save your progress.