Sub topic 6
Hover, active and visited states
This chapter assumes you know basic CSS selectors, pseudo-classes, and how to inspect an element in your browser's developer tools. It builds on the button and link styles from earlier chapters.
Why states matter
A clickable element should respond to the user. When the pointer moves over it, when it is pressed, and when the user has already been to its destination, the element should change. These changes confirm that the element is interactive and that the page registered the action.
Each state answers a different question:
| State | Pseudo-class | Question it answers |
|---|---|---|
| Hover | `:hover` | Can I click this? |
| Active | `:active` | Did my press register? |
| Visited | `:visited` | Have I been here before? |
Keyboard focus is a fourth state. The next chapter covers it, so this chapter only notes where it fits in the rules.
Hover: the "yes, this is clickable" cue
The :hover pseudo-class matches while a pointer rests over an element. Keep the change small and clear. A slightly darker fill on a button or a thicker underline on a link is enough.
.btn {
background: #1d4ed8;
color: #ffffff;
border: 2px solid #1d4ed8;
transition: background-color 0.15s ease;
}
.btn:hover {
background: #1e40af;
border-color: #1e40af;
}Touch screens have no true hover. Many mobile browsers apply :hover after a tap and leave it on until the user taps elsewhere. Never put essential information behind hover. Hover is an extra cue and should never be the only one.
Active: the "I pressed it" cue
The :active pseudo-class matches while the element is being pressed, from mouse-down to mouse-up, or during a touch. It gives immediate feedback, which matters most when the action takes a moment to complete. A common pattern is a one-pixel shift that suggests the button is pushed in.
.btn:active {
transform: translateY(1px);
}Visited: the "you have been here" cue
The :visited pseudo-class applies to links whose address is in the browser history. It helps users in long lists of links, such as search results or documentation indexes, because they can see what they have already read.
a {
color: #1d4ed8;
text-decoration: underline;
}
a:visited {
color: #6d28d9;
}
a:hover {
color: #1e3a8a;
text-decoration-thickness: 2px;
}Browsers limit what :visited can change, to stop pages from detecting a user's history. You can change colors, such as color and background-color. Other properties are ignored or reported as unvisited. Keep both the visited and unvisited colors above the contrast ratio you chose earlier, and keep them clearly different from each other.
Buttons do not have a visited state. It applies to links only, so do not style .btn:visited on real buttons.
Rule order matters
These pseudo-classes have equal specificity, so the later rule wins when two match at the same time. A link that is both visited and hovered will show the visited color if :visited comes after :hover. The usual safe order is:
a(the base style)a:visiteda:hovera:focus-visiblea:active
This order is often remembered as "LVHFA", an extension of the older "LVHA" mnemonic that starts with :link. Because a already covers unvisited links, you can omit :link.
Common errors
- Hover only on desktop. Leaving out an
:activestyle means touch users get no press feedback. - Changes too large. Layout shifts on hover, such as a change in padding or border width, make neighbors jump. Change color or use
transforminstead. - Weak contrast in a state. A hover color that is lighter than the base color can fall below the contrast ratio. Check every state, not only the default.
- Visited color too close to the default. Users cannot tell the two apart.
- Instant, flickery changes. A short
transitionof 100 to 200 milliseconds makes changes feel smooth. Keep it brief so the interface does not feel slow.
Respect users who prefer less motion
Small transitions are usually fine. If you add larger movement, wrap it in a media query so users who ask for reduced motion do not get it.
@media (prefers-reduced-motion: reduce) {
.btn {
transition: none;
}
.btn:active {
transform: none;
}
}Exercise
A link has these rules in this order: a:hover { color: #1e3a8a; } and then a:visited { color: #6d28d9; }. A user hovers over a link they have already visited. Which color do they see, and how do you fix the rule order so the hover color shows?
Show answer
They see the visited color (#6d28d9), because both rules have equal specificity and the later one wins. Move the a:visited rule before the a:hover rule so that hover overrides it.
Exercise
Why should you avoid changing a button's border width on hover, and what could you change instead?
Show answer
A larger border changes the element's size and pushes nearby content, causing a layout shift. Change the background color, border color, or use transform instead, since these do not affect the layout.