Sub topic 6
Hover, active and visited states
This chapter covers the states that tell a user how a button or link is responding to them. It assumes you know basic HTML, CSS selectors and pseudo-classes, the box model, and how to inspect an element in your browser's developer tools. Focus indicators for keyboard users and disabled and loading states have their own chapters. Here we cover hover, active and visited, and how they fit together.
Why states matter
A control that looks clickable at rest has done half its job. The other half is feedback. When the pointer moves over a control, the user wants to know it is the one they are about to press. When they press, they want to know the press registered. When they come back to a page, they want to know which links they have already followed.
Each of these three moments has its own CSS pseudo-class:
| State | Pseudo-class | What the user is doing | What the cue tells them |
|---|---|---|---|
| Hover | `:hover` | Pointer rests over the element | "This is interactive and this is the one you will activate." |
| Active | `:active` | Pointer button or finger is held down on the element | "Your press registered." |
| Visited | `:visited` | Nothing, the link's URL is in the browser history | "You have been here before." |
Hover and active apply to buttons and links alike. Visited applies only to links, because it depends on browser history.
Hover
The :hover pseudo-class matches while a pointing device is over an element. A good hover change is small and clearly visible. Typical changes are a slightly darker or lighter background, a thicker underline, or a small shadow.
Here is the filled button from the earlier chapter, with its hover rule highlighted by a comment:
.btn {
display: inline-flex;
align-items: center;
justify-content: center;
min-height: 44px;
padding: 0 1.25rem;
border: 2px solid #1d4ed8;
border-radius: 8px;
background: #1d4ed8;
color: #ffffff;
font: inherit;
font-weight: 600;
text-decoration: none;
cursor: pointer;
}
/* Hover: a darker blue of the same hue */
.btn:hover {
background: #1e40af;
border-color: #1e40af;
}Notice that the border color changes along with the background. If you change only the background, the old border stays and draws a visible ring around the new fill.
The cursor: pointer declaration is part of the hover story. The browser shows the pointing-hand cursor by default over links. It does not do so over <button> elements. Setting it on buttons keeps the two consistent. Do not put it on elements that are not interactive, because it promises something that is not there.
Hover cannot be the only cue
Touch screens have no hover. On most phones, a tap may trigger a brief, sticky :hover state, or none at all. Keyboard users never trigger it. So hover can only reinforce a cue that is already present at rest. If an element looks like plain text until you hover over it, touch and keyboard users will never learn that it is clickable. The earlier chapters covered the resting cues: fill, border, shape and underline. Hover adds to them.
You can restrict hover styling to devices that really support it with a media query:
@media (hover: hover) and (pointer: fine) {
.btn:hover {
background: #1e40af;
border-color: #1e40af;
}
}This avoids the sticky-hover effect on touch devices, where a button can stay dark after a tap until the user taps somewhere else. The trade-off is that some hybrid devices report themselves in ways that skip your hover rule. Use this pattern when sticky hover is a problem you can observe. It is not required for every project.
Transitions
A short transition smooths the change and makes it easier to notice. Keep it brief.
.btn {
transition: background-color 120ms ease, border-color 120ms ease;
}
@media (prefers-reduced-motion: reduce) {
.btn {
transition: none;
}
}A color fade of about 100 to 150 milliseconds feels responsive. Much longer, and the control feels slow to react. The prefers-reduced-motion query respects users who have asked their system to minimize animation. A color fade is mild, but removing it for those users is cheap and safe.
Active
The :active pseudo-class matches while the element is being pressed: from the moment the mouse button or finger goes down until it is released. On a keyboard, a focused <button> also matches :active while Space is held down in most browsers.
The active state is the physical "click" of the interface. Common treatments are:
- A slightly darker fill than hover, so the sequence rest, hover, active steps in one direction.
- A one-pixel downward shift, which suggests the button moves under the finger.
- Removing a shadow, which suggests the button is pressed flat.
.btn:active {
transform: translateY(1px);
}The shift is small on purpose. A large movement makes the layout feel unstable, and because transform does not affect layout, nothing around the button moves.
Rule order matters
The pseudo-classes :hover and :active have the same specificity. When both match, the one written later wins. A user who presses a button is still hovering over it, so both rules apply at once. If :active comes before :hover in your stylesheet, the hover style overrides it and the press feedback never appears.
/* Wrong order: :hover hides the :active color while pressing */
.btn:active { background: #1e3a8a; }
.btn:hover { background: #1e40af; }
/* Right order: :active comes after :hover */
.btn:hover { background: #1e40af; }
.btn:active { background: #1e3a8a; }For links, the full order is usually remembered as LVHA: :link, :visited, :hover, :active. Each rule that comes later takes priority over the ones before it when they match together.
Making the whole control respond
Make sure the pressable area is the element itself, not just its text. If a link is wrapped around an icon with padding on an inner element, the active state can flicker as the pointer crosses between parent and child. Put the padding and the state styles on the element that carries the href or the <button> tag.
Visited
The :visited pseudo-class matches links whose destination is in the user's browsing history. It exists so that readers can tell which results of a search or entries in a list they have already opened. It is most useful in link-heavy pages such as documentation indexes, article lists and search results. It adds little in a navigation bar, where every link is visited constantly.
Here is the link style from the earlier chapter:
a {
color: #1d4ed8;
text-decoration: underline;
text-decoration-thickness: 1px;
text-underline-offset: 0.15em;
}
a:visited {
color: #6d28d9;
}
a:hover {
color: #1e3a8a;
text-decoration-thickness: 2px;
}Blue to purple is the long-standing convention, and users know it. Keep the visited color clearly different from the unvisited color, but still readable against the background. A visited link that fades to light gray may fail contrast requirements and looks disabled.
Privacy limits on `:visited`
Browsers restrict what :visited can do, because a page could otherwise read a user's history by testing which links are styled as visited. Only these properties can be changed:
colorbackground-colorborder-colorand its side-specific variantsoutline-colortext-decoration-color- Fill and stroke colors in SVG
Other properties are ignored. Any alpha value in a color is also treated as opaque. Scripts that ask for a visited link's computed style get the unvisited values. So you cannot change the underline thickness, add an icon or hide a visited link through :visited. Plan your visited cue around color.
Visited and hover together
A hovered visited link matches both :visited and :hover. Because :hover is later in the order, it wins on any property both set. That is usually what you want: the hover color applies to all links, visited or not. If you want a distinct hover color for visited links, write a:visited:hover after the other rules.
a:visited:hover {
color: #4c1d95;
}Buttons and visited
Do not give a link-styled button a different look for visited. A "Create an account" link that turns purple after one click looks as if the user has already done it. Use :visited for links in running text and lists, and reset it for button-styled links:
a.btn:visited {
color: #ffffff;
}
a.btn.btn--secondary:visited {
color: #1d4ed8;
}Without this, the general a:visited rule would change the label color on a filled button and could make it hard to read against the dark background. Because a:visited has a specificity of one class-like pseudo plus one element, the button reset needs to be at least as specific and come later.
Putting the states together
The three states run in sequence during a normal interaction. This diagram shows how a control moves between them.
For a link that has been followed before, the resting state is the visited color, and hover and active layer on top of it.
A complete, small example for links in running text:
a {
color: #1d4ed8;
text-decoration: underline;
text-decoration-thickness: 1px;
text-underline-offset: 0.15em;
}
a:visited {
color: #6d28d9;
}
a:hover {
color: #1e3a8a;
text-decoration-thickness: 2px;
}
a:active {
color: #172554;
}Common errors
- Hover as the only cue. The element looks like text until the pointer is over it. Touch and keyboard users never see the cue. Give it a resting signifier.
- Wrong rule order.
:activeplaced before:hovernever shows. Use LVHA. - Changing layout on hover. Adding a border or increasing padding on hover moves neighbouring content. Reserve the space at rest, for example with a transparent border, or use
transform,box-shadoworoutline, which do not affect layout. - Hover styles that are too subtle. A change from
#1d4ed8to#1e4ed9is invisible. Aim for a difference people can see at a glance. - Removing the underline on hover for links in text. The cue that identified the link disappears at the moment the user is aiming for it. Thicken or darken instead.
- Trying to restyle
:visitedbeyond color. The browser ignores the property without warning. - Visited styling on button-styled links. It makes a call-to-action look used.
- Feedback only on hover for pointer users. The press itself has no response, so the interface feels dead on touch. Add
:active.
Checking your work
You can test these states without a mouse by using your browser's developer tools. In most browsers, the Elements or Inspector panel lets you right-click an element, or open the styles toolbar, and force a state such as :hover, :active or :focus-visible. Use this to compare the three states side by side.
Then check the following:
- Each state differs from the one before it by a visible amount.
- The text keeps enough contrast in every state, including visited.
- Nothing around the control moves when the state changes.
- On a phone or a touch emulator, tapping does not leave the control stuck in its hover color.
Exercise
A stylesheet contains a:hover { color: #1e3a8a; } followed by a:visited { color: #6d28d9; }. A user hovers over a link they have already visited. What color do they see, and how do you fix the rules so the hover color shows?
Show answer
They see purple (#6d28d9). Both rules match, they have equal specificity, and the later rule (a:visited) wins. Reorder the rules so a:visited comes before a:hover, following LVHA (link, visited, hover, active). Then a hovered visited link shows the hover color.
Exercise
You want visited links in a list of search results to show a small checkmark icon after the link text, using a:visited::after. Will this work, and what should you do instead?
Show answer
No. Browsers restrict :visited to color-related properties, so generated content, size and layout changes are ignored. Use a visited color change, which is allowed. If you truly need a history-based icon, you must track visits yourself on the server or in your own storage, not through CSS.
Exercise
A button grows a 2px border on hover and the text next to it shifts sideways. Why does it happen, and what are two ways to avoid it?
Show answer
The border adds to the box size, so the layout reflows. Either give the button a 2px transparent border at rest and change only its color on hover, or use a property that does not affect layout, such as box-shadow, outline or transform.