p {
color: red;
}
header {
color: white;
background-color: black;
}
We'll split the selectors into:
* | Matches all elements |
---|---|
E | Matches all elements with tag name E |
#I | Matches the element with id of I |
.C | Matches all elements with class of C |
E > F | Matches all F elements that are children of E elements |
E F | Matches all F elements that are descendants of E elements |
E + F | Matches all F elements that are immediately preceded by sibling E |
E ~ F | Matches all F elements preceded by sibling E |
* | Matches all elements |
---|---|
E | Matches all elements with tag name E |
#I | Matches the element with id of I |
.C | Matches all elements with class of C |
E > F | Matches all F elements that are children of E elements |
E F | Matches all F elements that are descendants of E elements |
E + F | Matches all F elements that are immediately preceded by sibling E |
E ~ F | Matches all F elements preceded by sibling E |
This will be our running example:
<p>
The next few sections tell you all about me.
</p>
<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>
<p>
But playing Grand Theft Auto
discouraged me from a life of car-jacking.
</p>
</section>
The universal selector * selects all elements:
* {
color: red;
}
* | Matches all elements |
---|---|
E | Matches all elements with tag name E |
#I | Matches the element with id of I |
.C | Matches all elements with class of C |
E > F | Matches all F elements that are children of E elements |
E F | Matches all F elements that are descendants of E elements |
E + F | Matches all F elements that are immediately preceded by sibling E |
E ~ F | Matches all F elements preceded by sibling E |
A selector can be a tag:
p {
color : red;
}
* | Matches all elements |
---|---|
E | Matches all elements with tag name E |
#I | Matches the element with id of I |
.C | Matches all elements with class of C |
E > F | Matches all F elements that are children of E elements |
E F | Matches all F elements that are descendants of E elements |
E + F | Matches all F elements that are immediately preceded by sibling E |
E ~ F | Matches all F elements preceded by sibling E |
id
Attributesrc
with img
).id
, are global, i.e. they apply to all elements (e.g. lang
).id
attribute's value, but it should be unique within the document, e.g.:
<section id="bio">
In CSS, you can use # to select by id:
#bio {
color: red;
}
* | Matches all elements |
---|---|
E | Matches all elements with tag name E |
#I | Matches the element with id of I |
.C | Matches all elements with class of C |
E > F | Matches all F elements that are children of E elements |
E F | Matches all F elements that are descendants of E elements |
E + F | Matches all F elements that are immediately preceded by sibling E |
E ~ F | Matches all F elements preceded by sibling E |
class
Attributeclass
is another global attribute.
<i class="game">Tekken 3</i>
In CSS, you can use . to select by class:
.game {
color: red;
}
* | Matches all elements |
---|---|
E | Matches all elements with tag name E |
#I | Matches the element with id of I |
.C | Matches all elements with class of C |
E > F | Matches all F elements that are children of E elements |
E F | Matches all F elements that are descendants of E elements |
E + F | Matches all F elements that are immediately preceded by sibling E |
E ~ F | Matches all F elements preceded by sibling E |
<!DOCTYPE html>
<html>
<head>
<title>A simple document</title>
</head>
<body>
<p>
Some words.
</p>
<p>
More words
<em>and emphasised words</em>
and final words.
</p>
</body>
</html>
Based on a parent
E.g. to select p
elements that are children of section
elements:
section > p {
color: red;
}
Based on an ancestor
E.g. to select i
elements that are descendants of section
elements:
section i {
color: red;
}
* | Matches all elements |
---|---|
E | Matches all elements with tag name E |
#I | Matches the element with id of I |
.C | Matches all elements with class of C |
E > F | Matches all F elements that are children of E elements |
E F | Matches all F elements that are descendants of E elements |
E + F | Matches all F elements that are immediately preceded by sibling E |
E ~ F | Matches all F elements preceded by sibling E |
Based on an immediately preceding sibling
E.g. to select a p
whose immediately preceding sibling is an h2
:
h2 + p {
color: red;
}
Based on any preceding sibling
E.g. to select a p
any of whose preceding siblings is an h2
:
h2 ~ p {
color: red;
}
We can have more than one selector, separated by commas.
E.g. these mean the same:
h2 {
color: red;
}
p {
color: red;
}
h2, p {
color: red;
}
E[foo]
finds E
elements that have an attribute foo
.E:first-child
finds E
elements that are the first child of their
parent.
E:hover
finds E
elements, but only when the user hovers over them with the mouse.E::first-letter
finds the first letter of E
elements.
<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>
li li {
color: red;
}
li > li {
color: red;
}
What do these select?
.ingredient + .ingredient
.ingredient ~ .ingredient
What do these select?
.ingredient:first-child
.ingredient:first-child + .ingredient