edubun
English
1 / 13

2 / 9 · Basic

Selectors and the cascade

Picture one paragraph with two rules: one says blue, the other says red. CSS does not guess. It scores both selectors and the higher score wins.

Jump to the 10 practice questions

You will learn

  • Select elements by type, class, id and combination
  • Work out which rule wins with specificity and order
  • Use inheritance instead of repeating declarations

Before this: What CSS does

1Explain

Two rules, one paragraph

Here is the conflict this unit solves. The page has <p class="note">Hi</p>. One rule colours every paragraph blue, and another colours everything with the class note red. The paragraph cannot be both. It turns red, because a class selector scores higher than a type selector. The rest of the unit shows how selectors are written and how that score is counted.

Your code
p { color: blue; }        /* score 1 */
.note { color: red; }     /* score 10: wins */
Preview

2Explore

Choosing elements

A selector is a pattern that matches elements in the page. The simplest ones match by element name, by class or by id. p is a type selector and matches every paragraph. .note is a class selector and matches every element with class="note"; one element can have several classes separated by spaces, and one class can be used on many elements.

Pick a selector and see which elements turn yellow.

Tap a choice: the code and the preview change together.

Selector
<style>
  p { background: #ffe08a; }
</style>
<h2>Today</h2>
<p class="note">Fresh rye</p>
<p id="left">1 task left</p>
<nav><a href="#">Bread</a> <a class="active" href="#">Cakes</a></nav>
Preview

3Problem

Matching class="note"

Which selector matches <p class="note">?

Hint

A class selector starts with a dot.

Solution

Answer: .note

A dot selects by class; a hash selects by id.

4Problem

nav a

What does nav a match?

Hint

A space means "anywhere inside".

Solution

Answer: Links anywhere inside a nav

A space is the descendant combinator; > would mean direct children.

5Explore

When rules disagree

First, which selector is more specific? Specificity is a score. Count an id as 100 points, a class as 10 and an element name as 1. In the opening example p scores 1 and .note scores 10, so red wins. A selector such as #menu a has one id and one element name, so it scores 101 and beats .active at 10.

The more specific selector wins. When both score the same, the later rule wins.

Tap a choice: the code and the preview change together.

First rule
Second rule
<style>
  a { color: red; }
  a { color: blue; }
</style>
<nav id="menu"><a class="active" href="#">Bread</a></nav>
Preview

6Problem

Score #menu .item a

What is the specificity of #menu .item a?

Hint

Count ids, then classes, then element names.

Solution

Answer: 1-1-1

One id, one class, one element name.

7Problem

Equal specificity

Two rules have equal specificity. Which wins?

Hint

Order decides.

Solution

Answer: The one later in the stylesheet

Source order breaks the tie.

8Explain

Inheritance

Some properties pass from parent to child automatically. Set color or font-family on body and every paragraph, list and table cell inside picks it up, unless a rule sets its own. Text-related properties generally inherit; box-related ones such as width, padding, border and margin do not, which is why a border on a section does not appear around every paragraph inside it.

Your code
<style>
  body { color: #1a5fb4; font-family: Georgia, serif; }
</style>
<p>Paragraphs inherit the colour and font from body.</p>
<ul><li>So do list items.</li></ul>
Preview

9Problem

An inherited property

Which property is inherited by default?

Hint

Set it on body and the text inside follows.

Solution

Answer: color

Text properties such as color and font-family inherit, so children use their parent’s values unless another rule sets their own.

10Practice

Class and id selectors

Use a class selector to strike through the finished tasks (text-decoration: line-through) and colour them #767676. Use an id selector to make only #left bold (font-weight: 700).

Checks
  • done tasks struck through
  • done tasks grey
  • open task not struck
  • #left bold
  • other paragraph normal
Your code
<style>
  /* write your rules here */
</style>

<h2>Today</h2>
<ul class="tasks">
  <li class="done">Buy flour</li>
  <li class="done">Feed the starter</li>
  <li>Bake bread</li>
</ul>
<p id="left">1 task left</p>
<p>Back at 3 pm.</p>
Preview
Hint

.done matches only the finished tasks; #left matches one paragraph.

A solution
<style>
  .done { text-decoration: line-through; color: #767676; }
  #left { font-weight: 700; }
</style>

<h2>Today</h2>
<ul class="tasks">
  <li class="done">Buy flour</li>
  <li class="done">Feed the starter</li>
  <li>Bake bread</li>
</ul>
<p id="left">1 task left</p>
<p>Back at 3 pm.</p>

11Practice

Make .active win

The Bread link should be red, but #menu a (1-0-1) beats .active (0-1-0). Without !important and without deleting the first rule, change the second selector so it wins.

Checks
  • active link red
  • other links black
  • first rule kept
Your code
<style>
  #menu a { color: black; }
  .active { color: red; }
</style>

<nav id="menu">
  <a href="#">Home</a>
  <a class="active" href="#">Bread</a>
  <a href="#">Cakes</a>
</nav>
Preview
Hint

#menu a scores 1-0-1. A selector with the id plus the class scores higher.

A solution
<style>
  #menu a { color: black; }
  #menu a.active { color: red; }
</style>

<nav id="menu">
  <a href="#">Home</a>
  <a class="active" href="#">Bread</a>
  <a href="#">Cakes</a>
</nav>

12Explain

Common mistakes

Mistake
<style>
  note { color: red; }
</style>
<p class="note">Fresh rye</p>
Fixed
<style>
  .note { color: red; }
</style>
<p class="note">Fresh rye</p>

Without the dot, note is read as an element name, and there is no note element.

13

Summary

  • p, .class, #id; comma groups; space means descendant; > means child.
  • Specificity ids-classes-types decides; ties go to the later rule.
  • Text properties inherit; box properties do not.

Next unitColour and units
Full text · The whole lesson as one text

Two rules, one paragraph

Here is the conflict this unit solves. The page has <p class="note">Hi</p>. One rule colours every paragraph blue, and another colours everything with the class note red. The paragraph cannot be both. It turns red, because a class selector scores higher than a type selector. The rest of the unit shows how selectors are written and how that score is counted.

p { color: blue; }        /* score 1 */
.note { color: red; }     /* score 10: wins */
Both rules match the same paragraph.

Choosing elements

A selector is a pattern that matches elements in the page. The simplest ones match by element name, by class or by id. p is a type selector and matches every paragraph. .note is a class selector and matches every element with class="note"; one element can have several classes separated by spaces, and one class can be used on many elements. #intro is an id selector and matches the single element with id="intro". *, the universal selector, matches everything.

p { line-height: 1.5; }            /* every p */
.note { background: #fff4d6; }     /* class="note" */
#intro { font-size: 20px; }        /* id="intro" */
The three basic selectors: type, class and id.

Combining selectors

h1, h2 { font-weight: 600; }       /* group: h1 and h2 */
nav a { text-decoration: none; }   /* a anywhere inside nav */
ul > li { margin: 4px 0; }         /* li that is a direct child of ul */
a:hover { text-decoration: underline; }
input[type="email"] { width: 20em; }
Grouped, combined, pseudo-class and attribute selectors.

A comma groups selectors that share the same declarations, so h1, h2 styles both. A space is the descendant combinator: nav a matches links anywhere inside a nav, however deep. > is stricter and matches only direct children. Writing two simple selectors together with no space, as in a.active, means one element must match both: a link that also has the class active. Attribute selectors in square brackets match by attribute, such as input[type="email"].

More ways to pick: states and parts

Pseudo-classes, written with one colon, select elements in a particular state or position. :hover matches while the pointer is over it, :focus-visible when it has keyboard focus, and :first-child when it is the first child of its parent. Pseudo-elements, written with two colons, style a part of an element, such as ::first-line or ::placeholder.

When rules disagree

Often several rules set the same property on the same element. The cascade, the C in CSS, decides which one wins. Leaving aside the browser's own styles and !important, two questions settle almost every case.

First, which selector is more specific? Specificity is a score. Count an id as 100 points, a class as 10 and an element name as 1. In the opening example p scores 1 and .note scores 10, so red wins. A selector such as #menu a has one id and one element name, so it scores 101 and beats .active at 10.

Browsers write the same score as three numbers: ids, then classes, then element names. #menu a is 1-0-1, .active is 0-1-0 and nav a is 0-0-2. Attribute selectors and pseudo-classes count as classes. The numbers are compared from the left, so one id beats any number of classes. The 100-10-1 weights are a good way to start, but they only work while no column passes 9.

For ordinary rules in the same cascade layer, equal specificity means the later rule wins. A cascade layer is a named group of rules with its own priority. These examples use unscoped rules from one source.

Inline style attributes beat any selector, and a declaration marked !important beats normal ones. Both are tempting quick fixes, but they make the next change harder, because the only way to override them is with more of the same. Most teams keep specificity low and even, styling mostly with classes, so that order in the file is enough to predict the result.

Inheritance

Some properties pass from parent to child automatically. Set color or font-family on body and every paragraph, list and table cell inside picks it up, unless a rule sets its own. Text-related properties generally inherit; box-related ones such as width, padding, border and margin do not, which is why a border on a section does not appear around every paragraph inside it. The keyword inherit forces a property to copy its parent's value.

Name classes by what something is, not by how it looks. .warning still makes sense when the designer changes the colour from red to orange; .red-text does not. Ids are unique, so they are best kept for link targets and form labels rather than for styling, which keeps specificity low.

Key points
  • p, .class, #id; comma groups; space means descendant; > means child.
  • Specificity ids-classes-types decides; ties go to the later rule.
  • Text properties inherit; box properties do not.

Practice questions from Selectors and the cascade on edubun

Embed this quiz

Free to embed. Please keep the credit line.

Sources for this course (5)
  • W3C CSS Working Group specifications: CSS Snapshot 2025, Selectors Level 4, CSS Cascading and Inheritance Level 5, CSS Color Level 4, CSS Values and Units Level 4, CSS Box Model Level 3, CSS Box Sizing Level 3, CSS Fonts Level 4, CSS Flexible Box Layout Level 1, CSS Grid Layout Level 2, Media Queries Level 4 and Level 5 (w3.org/TR/, checked 2026-10-17)
  • MDN Web Docs, Learn web development: CSS styling basics and CSS layout, and the CSS reference (developer.mozilla.org/en-US/docs/Learn_web_development/Core/Styling_basics, .../Core/CSS_layout, developer.mozilla.org/en-US/docs/Web/CSS, checked 2026-10-17)
  • W3C, CSS history and CSS1 Recommendation of 17 December 1996 (w3.org/Style/CSS20/history.html, w3.org/press-releases/1996/css1-rec, checked 2026-10-17)
  • W3C WCAG 2.2, success criteria 1.4.3 Contrast (Minimum)
  • 1.4.8 Visual Presentation, 1.4.10 Reflow, 1.4.12 Text Spacing (w3.org/TR/WCAG22/, checked 2026-10-17).

Checked: 2026-10-17