CSS Selectors!: Delegating and Refining
CSS Selectors!: Delegating and Refining
Introduction
So far we have used a very basic CSS selector. We
have referred to HTML items directly. That is only
the beginning however. Selectors can actually get
quite complex in terms of how we define which
content will get which rules applied to it. In this
section we won't cover all selectors (that would be
way too long and confusing). Instead we'll cover
some of the more useful selectors and create a
base from which you should easily be able to
explore and learn the others.
1
cases but you should be aware of the others so
you can come back and expand your knowledge
when you encounter scenarios which could take
advantage of them.
So What is a Selector?
We saw in an earlier section that the basic layout
of CSS rules is as follows:
selector {
property: value;
property: value;
}
Type Selectors
The type selector is the most basic selector. It is
the name of a HTML tag. When we use this
2
selector, every item in the HTML which is defined
by that tag will have the given properties applied to
it.
style.css
h1 {
font-family: Times New Roman;
}
p {
font-family: Verdana;
}
our_page.html
<body>
<h1>Where are average things built?</h1>
<p>In the satisfactory.</p>
</body>
Type Selectors
Where are average things built?
In the satisfactory.
3
Id's and Classes
Often, we would like finer grained control over
where our CSS rules will be applied. An effective
way to achieve this is by way of id's and classes.
We apply both of these to the HTML as attributes
on tags. The examples below will make it clearer.
Id's
An id is used to identify a single item in the HTML.
To do so we add an attribute to the relevant HTML
tag as follows;
<tagName id="idName">
#idName {
property: value;
property: value;
}
4
The id may be named with letters, digits,
underscores and dashes. It is best to name it as
something descriptive so you can easily identify
what it represents. You may have as many different
id's on a page as you like. You may apply an id to
any tag on the page.
style.css
#topmenu {
background-color: #cccccc;
}
our_page.html
<body>
<div id="topmenu" >Page1 | Page2 | Page 3</div>
<p>Page content goes here.</p>
</body>
Id Selectors
5
Page content goes here.
6
Classes
A class is similar to an id except it applies to multiple
items as opposed to just a single item. To make an
item on our page part of a class we add the class
attribute to its tag like so:
<tagName class="className">
.className {
property: value;
property: value;
}
7
style.css
.important {
font-weight: bold;
border: 2px #cc2244 solid;
}
our_page.html
<body>
<p class="important">Important: When everything is
important, nothing is important.</p>
</body>
9
Descendant Elements
With descendant elements we can say that any
items within another type of item should get a set of
properties applied to them. We do this by writing the
parent selector, followed by the child selector like
so:
parentSelector childSelector {
property: value;
property: value;
}
style.css
.important {
font-weight: bold;
border: 2px #cc2244 solid;
}
.important b {
color: #ff1122;
10
}
our_page.html
<body>
<p class="important">Important: When everything is
<b>important&tl;/b>, nothing is important.</p>
<p>And this is perfectly <b>normal</b>.</p>
</body>
Multiple Selectors
Sometimes you find yourself repeating the same
set of properties and values for several different
selectors. To make life easier, you can put those
properties into their own block and specify that it
should apply to multiple selectors at once. To do
this, list all the selectors, separated by a comma
like so:
11
selector1, selector2, selector3 {
property: value;
property: value;
}
Hover
You have most likely seen the hover selector used
to make links change when you hover your mouse
over them. We identify a set of properties to be
applied when the user hovers their mouse over an
item by adding the keyword :hover to the end of
the selector.
12
selector1:hover {
property: value;
property: value;
}
style.css
p:hover {
padding-left: 30px;
color: #3366cc;
}
our_page.html
<body>
<p>Hover over me. I dare you!</p>
</body>
13
With a bit of creativity you can create some really
interesting effects using hover.
Pseudo Elements
Pseudo Elements are not specific items on the
page but virtual items you may target.
First Line
It is possible to change the first line of content for
an item using the ::first-line keyword (Note: 2
colons as opposed to 1 for hover).
selector1::first-line {
property: value;
property: value;
}
First Letter
14
As well as the first line, we may also target the first
letter. This is done using the ::first-letter keyword.
selector1::first-letter {
property: value;
property: value;
}
selector1::before {
content: value;
property: value;
property: value;
}
15
When using these, we include the property content
which sets what the content of this pseudo
element should be. The value could be a string:
Or it could be an image:
content: url(/https/www.scribd.com/path/to/image.jpg);
Or it could be a counter:
content: counter(li);
16
our_page.html
<body>
<p id="topmenu"> <a>Page1</a> <a>Page2</a>
<a>Page3</a> </p>
</body>
Overlapping Rules
With all these different selectors at your disposal,
it is quite possible that a particular item in your
HTML will have multiple selectors applied to it and
that within those selectors there may be clashes in
terms of properties. How the browser determines
which property values actually get applied can be
a bit tricky and involves specificity, inheritance and
cascading.
17
Specificity is the most important to understand and
has to do with how directly the selector relates to
the item. In general, from least direct, to most
direct, they are:
type
class
id
It can get more complex once descendants and
other mixes come into play.
style.css
p {
color: #3366cc;
18
}
.whatcolor {
color: #66cc33;
}
#whatcolor {
color: #cc6633;
}
our_page.html
<body>
<p class="whatcolor" id="whatcolor">What colour am
I?</p>
</body>
Overlapping Rules
What colour am I?
Best Practice
With several different selectors and an almost
endless combination of ways they can be combined
and applied it may seem like a daunting task to work
out how best to structure your code. Working out
the best way to do things should come naturally with
19
time and experience as long as you are
experimenting and thinking about this as you go.
Don't worry if your first attempts seem clunky and
inefficient. Also, don't be afraid to rework your code
when you get stuck.
20
clean and efficient CSS but then your HTML will
require many classes applied to many items and so
we have lost that separation. Getting the right
balance is something you will work out for yourself
over time with experience.
21