Sub topic 4
Link states and the LVHA order
This chapter assumes you know basic HTML, CSS rule syntax, and type and class selectors. It also assumes you know what a pseudo-class is and that it starts with one colon. The chapter covers how links change between states, why the order of your link rules matters, and what limits browsers put on styling visited links.
The states a link can be in
A link is an <a> element with an href attribute. At any moment it can be in several states at once.
| Pseudo-class | Matches when |
|---|---|
| `:link` | The link has an `href` and the user has not visited its target. |
| `:visited` | The link has an `href` and its target is in the browser history. |
| `:hover` | A pointing device is over the element. |
| `:focus` | The element has keyboard or programmatic focus. |
| `:active` | The element is being activated, for example while the mouse button is held down. |
| `:any-link` | The link matches either `:link` or `:visited`. |
Every link with an href is in exactly one of :link or :visited. It can also be hovered, focused, and active at the same moment. So a visited link under the mouse matches :visited and :hover together.
An <a> with no href is a placeholder. It matches neither :link nor :visited. It still matches :hover, because hover applies to any element.
Why order matters
Here is the basic set of link rules.
a:link {
color: #0645ad;
}
a:visited {
color: #6b3fa0;
}
a:hover {
text-decoration: underline;
}
a:active {
color: #c00;
}All four selectors have the same specificity: one type selector and one pseudo-class, so (0,1,1). When two rules of equal specificity set the same property, the later rule wins. That is the only reason the order matters.
The order above is called LVHA: :link, :visited, :hover, :active. A common mnemonic is "LoVe HAte".
The order follows from which states can overlap:
- A link is always
:linkor:visited. Those are the baseline, so they go first. - A link can be
:visitedand:hoverat once.:hovergoes after:visited, so a hover rule can override the visited colour. - A link can be
:hoverand:activeat once. While you press the mouse button, you are still hovering.:activegoes last so its colour wins during the press.
What goes wrong with a different order
Suppose you swap the first two rules and put :hover colour before :visited:
a:link {
color: #0645ad;
}
a:hover {
color: #d35400;
}
a:visited {
color: #6b3fa0;
}For an unvisited link, hover works. For a visited link, both a:hover and a:visited match and have equal specificity. a:visited comes later, so it wins. The hover colour never shows on visited links. This is a common bug, and it only appears after you have visited a link, so it is easy to miss while developing.
Adding focus: LVHFA
Keyboard users move through links with Tab. The link then matches :focus, not :hover. You should give focus its own styling, and many authors extend the order to LVFHA, or place :focus just before :hover:
a:link { color: #0645ad; }
a:visited { color: #6b3fa0; }
a:focus { outline: 3px solid #1a73e8; outline-offset: 2px; }
a:hover { text-decoration: underline; }
a:active { color: #c00; }Both :focus and :hover placements work if they set different properties, as above, because there is no conflict to resolve. The order only decides which rule wins when two rules set the same property. If you want hover to override focus colours, put :hover after :focus. If you want focus to stay visible even while the pointer is over the link, put :focus after :hover. The next chapter treats :focus and :focus-visible in detail. For now, just do not leave keyboard focus unstyled.
Making the rules shorter
Most sites do not need a different base colour for visited and unvisited links, so two shorter patterns are common.
Style plain `a` first, then override
a {
color: #0645ad;
}
a:visited {
color: #6b3fa0;
}
a:hover,
a:focus {
color: #d35400;
}
a:active {
color: #c00;
}The plain a selector has specificity (0,0,1), lower than the pseudo-class rules, so order among the first rule and the others does not matter. But a:visited, a:hover, and a:active still need to keep that relative order. Here a:hover comes after a:visited, so hover wins on visited links. Good.
Use `:any-link`
:any-link matches any link with an href, visited or not. It lets you target links without matching placeholder anchors:
a:any-link {
text-decoration-thickness: 0.08em;
text-underline-offset: 0.15em;
}Specificity traps with classes
A class selector has specificity (0,1,0). a:link has (0,1,1), which is higher. That means this does not work as many people expect:
.button {
color: white;
background: #1a73e8;
}
a:link {
color: #0645ad;
}On <a class="button" href="/go">, the link text is blue, not white. a:link beats .button on specificity, regardless of order. Fixes:
- Raise the class rule:
a.button { ... }has (0,1,1), and it wins here because it comes later. - Lower the link rule with
:where()::where(a):linkhas (0,1,0), so the later or more specific rule wins as usual. - Style
.button:link, .button:visitedexplicitly when the button must keep its colour in both states.
Limits on styling `:visited`
Browsers restrict :visited for privacy. If a page could read the real style of a link, a malicious page could test which sites you have visited. So browsers apply these rules:
- Only colour-related properties work in a
:visitedrule:color,background-color,border-color(and its sides),outline-color,column-rule-color,text-decoration-color, andfillandstrokein SVG. - Properties such as
font-weight,padding,background-image, orcontentare ignored in:visited. Changingdisplayor size could leak history through layout. getComputedStyle()reports the unvisited style, even for a visited link.- Selectors that combine
:visitedwith other parts are limited. A rule like:visited spancan only style the link itself.
In practice, treat :visited as a way to change colours and nothing more.
A complete example
This page runs as written. Save it and open it in a browser. Click the first link, return with the Back button, and hover over both links.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Link states</title>
<style>
a:link { color: #0645ad; }
a:visited { color: #6b3fa0; }
a:focus { outline: 3px solid #1a73e8; outline-offset: 2px; }
a:hover { color: #d35400; text-decoration: underline; }
a:active { color: #c00; }
</style>
</head>
<body>
<p><a href="https://example.com/">A real link</a></p>
<p><a>An anchor with no href</a></p>
</body>
</html>The first link shows all five states. The second matches only :hover, so it gets the orange hover colour and underline but never the blue base colour.
Summary of rules to follow
- Write link rules in the order
:link,:visited,:hover,:active, and place:focusbefore:hoverunless you have a reason not to. - Remember that equal specificity means the later rule wins, and that is why the order matters.
- Cover both
:linkand:visitedwhen styling classed links, or use:any-link. - Change only colours in
:visited. - Never leave keyboard focus without a visible style.
Exercise
A site has these rules, in this order: a:link { color: blue; }, a:hover { color: orange; }, a:visited { color: purple; }. A user hovers over a link they have already visited. What colour is the link, and how would you fix the order so hover works on visited links?
Show answer
The link is purple. a:hover and a:visited both match and have equal specificity (0,1,1), so the later rule, a:visited, wins. To fix it, move a:visited before a:hover, giving the order :link, :visited, :hover, :active.
Exercise
You write .btn { color: white; } and a:link { color: #0645ad; } in any order, and <a class="btn" href="/x"> still shows blue text. Why, and name one fix.
Show answer
a:link has specificity (0,1,1) and .btn has (0,1,0), so a:link wins regardless of order. Fixes include writing a.btn:link, a.btn:visited { color: white; }, or lowering the link rule's specificity with :where(a):link.