Sub topic 1
Selectors you already use, and where pseudo selectors fit
Prerequisites
This chapter assumes you can already:
- Read and write basic HTML elements and attributes.
- Write a CSS rule with a selector, properties, and values.
- Use type, class, ID, and attribute selectors.
- Use the descendant and child combinators.
What a selector does
A selector is the part of a CSS rule before the braces. It tells the browser which elements in the document the declarations apply to. The browser checks every element against the selector and applies the rule to each one that matches.
Every selector you have used so far matches on something written in the HTML itself: a tag name, a class, an ID, an attribute, or a position in the element tree.
Selectors you already know
| Kind | Example | Matches |
|---|---|---|
| Type | `p` | Every `<p>` element |
| Class | `.card` | Elements with `class="card"` |
| ID | `#main` | The element with `id="main"` |
| Attribute | `a[target="_blank"]` | Links that open in a new tab |
| Descendant | `nav a` | Any `<a>` inside a `<nav>` |
| Child | `ul > li` | `<li>` elements directly inside a `<ul>` |
All of these test what is written in the markup. They cannot tell you whether a user is pointing at an element, whether a checkbox is ticked, or whether an element is the third item in a list. They also cannot reach the first letter of a paragraph, because that letter has no tag.
Where pseudo selectors fit
Pseudo selectors fill those two gaps. They come in two families.
- A pseudo-class selects an element that is in a particular state or position. Examples are
:hover(the pointer is over it) and:first-child(it is the first child of its parent). It is written with one colon. - A pseudo-element selects a part of an element, or content the browser generates, that has no HTML tag of its own. Examples are
::first-letterand::before. It is written with two colons.
You attach a pseudo selector to a selector you already know. The first selector picks the element, and the pseudo selector narrows the match.
/* Type selector narrowed by a state */
a:hover {
color: #c00;
}
/* Class selector narrowed by a position */
.menu li:first-child {
font-weight: bold;
}
/* Type selector plus a pseudo-element */
p::first-letter {
font-size: 2em;
}Read a:hover as "any <a> element, but only while the pointer is over it". The rule stops matching when the pointer leaves. The browser re-evaluates pseudo-classes as the document changes, so you do not write any JavaScript to switch the style on and off.
Common mistakes at this stage
- Putting a space before the colon.
a :hovermeans "any element in state hover inside ana", because the space is a descendant combinator. Writea:hoverwith no space. - Treating the two families as interchangeable. Pseudo-classes filter which elements match. Pseudo-elements choose a different target.
The next chapter covers the one-colon and two-colon difference in detail.
Exercise
In the selector .menu li:first-child, which part is the pseudo-class, and what does the whole selector match?
Show answer
:first-child is the pseudo-class. The whole selector matches an <li> that is the first child of its parent and sits inside an element with the class menu.