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 questionsYou 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.
p { color: blue; } /* score 1 */
.note { color: red; } /* score 10: wins */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.
<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>Nothing is drawn: there is no visible content.
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.
<style>
a { color: red; }
a { color: blue; }
</style>
<nav id="menu"><a class="active" href="#">Bread</a></nav>Nothing is drawn: there is no visible content.
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.
<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>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).
- done tasks struck through
- done tasks grey
- open task not struck
- #left bold
- other paragraph normal
<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>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.
- active link red
- other links black
- first rule kept
<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>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
<style>
note { color: red; }
</style>
<p class="note">Fresh rye</p><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.