This will be our running example:
<section id="bio">
<h2>Biography</h2>
<p>
I was inspired to take up <i>Taekwondo</i> after
playing the video game <i class="game">Tekken 3</i>.
</p>
</section>
Often the same element may be selected by more than one rule.
h2 {
color: red;
}
h2 {
font-style: italic;
}
h2 {
color: red;
}
h2 {
color: blue;
}
The problem is often less obvious, e.g.
#bio {
color: blue;
}
section {
color: green;
}
For a given property, two or more CSS rules select an element and set different values
Resolution uses three criteria, taken in this order:
But note that this is a simplified explanation.
em
& i
elements are in italic;strong
, b
& h1
-h6
elements are bold;h1
headings are bigger than h2
headings;If two rules conflict and one is from an author stylesheet and the other is from the user-agent stylesheet, then the author stylesheet wins.
If origin does not resolve a conflict, then we turn to specificity.
The more specific rule wins.
i {
color: blue;
}
.game {
color: green;
}
The class selector is more specific.
section p {
color: yellow;
}
p {
color: gray;
}
The descendant selector is more specific.
#bio i {
color: teal;
}
section p i {
color: purple;
}
The id selector is more specific.
section p i {
color: teal;
}
p > i {
color: purple;
}
The longer selector is more specific.
If origin and specificity do not resolve a conflict, then we turn to ordering.
The later rule wins.
h2 {
color: red;
}
h2 {
color: blue;
}
The later selector wins.
link
elements is used.
"Taekwondo" and "Tekken 3" will be blue, not green!
#bio {
color: green;
}
p > i {
color: red;
}
section i {
color: blue;
}
section {
color: yellow;
}
<section id="bio">
<h2>Biography</h2>
<p>
I was inspired to take up <i>Taekwondo</i> after
playing the video game <i class="game">Tekken 3</i>.
</p>
</section>
i
elements to be blue, except ones referring to video games, which should be red.
i {
color: blue;
}
.game {
color: red;
}
<section id="bio">
<h2>Biography</h2>
<p>
I was inspired to take up <i>Taekwondo</i> after
playing the video game <i class="game">Tekken 3</i>.
</p>
</section>
<ul>
<li id="pastry">For the pastry:
<ul>
<li class="ingredient">flour</li>
<li class="ingredient">butter</li>
</ul>
</li>
<li id="filling">For the filling:
<ul>
<li class="ingredient">stewing beef</li>
<li class="ingredient">an onion</li>
<li class="ingredient">Guiness</li>
</ul>
Optionally, include <b class="ingredient">carrots</b>.
</li>
</ul>
What colour will "flour" be and why?
.ingredient {
color: red;
}
li {
color: blue;
}
What colour will "flour" be and why?
li {
color: red;
}
li li {
color: blue;
}
What colour will "flour" be and why?
ul > li > ul > li {
color: red;
}
#pastry li {
color: blue;
}
What colour will "flour" be and why?
ul > li > ul > li {
color: red;
}
ul li ul li {
color: blue;
}
What colour will "flour" be and why?
#pastry {
color: red;
}
ul li ul li {
color: blue;
}
What colour will "flour" be and why?
ul ul {
color: red;
}
li {
color: blue;
}
What colour will "flour" be and why?
#pastry {
color: red;
}
li {
color: blue;
}
Tip 1
You need to learn this stuff for the exam.
Tip 2
If you hover over a selector in VSCode, it shows you the specificity (as 3 numbers).
Tip 3
Chrome Developer Tools shows CSS that gets 'eliminated'.