Think of a porch light that only turns on when someone walks past. The light is the same. Only the situation changes. In CSS, a style can work like that porch light: it is only used in a certain situation. For example: while the mouse is over the element, while a text field is clicked, or when the element is the first child in its list. You write the situation with a pseudo-class: an addition that starts with one colon and is attached directly behind the selector, for example a:hover.
Change the colour while the mouse is over it
This example has one link and two rules for its colour:
<a href="#">Hover over me!</a>
<style>
a { color: blue; }
a:hover { color: red; }
</style>
a { color: blue; }gives the link its normal colour, blue.a:hoveris the selectoraplus the pseudo-class:hover, with no space. It means: the link, but only while the mouse is over it.{ color: red; }only applies while the mouse is over the link.
The result: the link is blue. When you move the mouse over it, the link turns red. When you move the mouse away, the link is blue again. Notice too that a:hover is written as a new rule below the a rule. It doesn't replace the a rule.
Two other common situations: :focus (a text field that is clicked or being typed in) and :active (an element that is being pressed).
Focus is the state of an element that is selected, by a click or the Tab key. There are two pseudo-classes for focus:
:focus: for text fields, whose style should show when they are clicked too.:focus-visible: for buttons and links. The style shows when they are reached with the keyboard, not when they are clicked. For example:button:focus-visible { outline: 3px solid orange; }(outlineis a line outside the border).
The browser draws its own focus line. Don't remove it with outline: none unless you add another style: with no focus line, keyboard users can't tell which element they are on.
Pick by position
A pseudo-class can also pick an element by its position inside its parent. These three rules pick list items <li>:
li:first-child { color: red; }
li:last-child { color: gray; }
li:nth-child(3) { font-weight: bold; }
li:first-childpicks the<li>that is the first child of its list.li:last-childpicks the<li>that is the last child.li:nth-child(3)picks the 3rd child. The number goes inside the parentheses.li:nth-child(2n)picks every second child: children 2, 4, 6 and so on.
In the exercise, a plain li { ... } rule is already there for all items. li:first-child { color: red; } only makes the first item red. The other items keep the colour from the li rule.
Pseudo-class names have to be written exactly. :first, :nth(1), :click and :over can't pick elements, so the browser ignores the rules that use them.
:first-child only matches when the element really is the first child. In a <div> that holds an <h2> and then some <p> elements, p:first-child picks nothing, because the first child is the <h2>. For the first <p>, use p:first-of-type: the first child among the children with the same tag.
⚠️ A hover that stays red, or never works
- Replacing the old rule. If you change
a { color: blue; }to red, the link is red all the time. Leave that rule and add a newa:hoverrule. - A space before the colon.
a :hovermeans something else: any element inside an<a>that is being hovered (the mouse is over it). Write it with no space:a:hover. - Hover on phones. Touch screens have no mouse, so don't show important information only on
:hover.
TL;DR: A pseudo-class is an addition to a selector that makes its rule apply only in a certain situation. It is written with one colon, attached with no space, for example a:hover. :hover applies while the mouse is over the element, :focus while a text field is clicked, and :active while the element is pressed. :first-child, :last-child and :nth-child(n) (child number n) pick an element by its position inside its parent. Write a pseudo-class as a new rule, don't replace the old rule.
⚡ A style while the mouse is over it
Write:
a { color: blue; }
a:hover { color: red; }
Meaning: :hover is a pseudo-class: an addition to a selector that makes its rule apply only in a certain situation. a:hover only applies while the mouse is over the link, so the blue link is red during that time. Write a:hover as a new rule below the a rule, don't replace the a rule.
⚡ Pick by position
Write:
li:first-child { color: red; }
li:last-child { color: gray; }
li:nth-child(3) { font-weight: bold; }
Meaning: These three pseudo-classes pick list items <li> by their position: :first-child picks the first child of its list, :last-child the last child, and :nth-child(3) the 3rd child. The other items don't change.
⚡ Stuck together, no space
Guess first: is a :hover (with a space) the same as a:hover?
No. With a space, it means: any element inside an <a> that is being hovered (the mouse is over it).
Meaning: A pseudo-class is written with one colon, attached directly behind its selector, with no space.