Sub topic 4
Link states and the LVHA order
This chapter assumes you know basic HTML, CSS rule syntax, type and class selectors, and the difference between a pseudo-class and a pseudo-element. Here you will style the four link states and learn why their order in the stylesheet matters.
The four link states
A link can be in several states at once. Four pseudo-classes cover the common ones:
| Pseudo-class | Matches |
|---|---|
| `:link` | An `<a href>` the user has not visited |
| `:visited` | An `<a href>` the browser has in its history |
| `:hover` | A link under the pointer |
| `:active` | A link while it is being pressed |
:link and :visited are mutually exclusive. :hover and :active are not. A visited link can also be hovered and pressed at the same time, so one element can match three of these rules at once.
An <a> without an href attribute is a placeholder link. It matches neither :link nor :visited.
Why order matters
All four pseudo-classes have the same specificity. Each adds one class-level unit to the selector. When two rules match the same element and have equal specificity, the rule that appears later wins.
Suppose you write :hover before :link. A hovered, unvisited link matches both rules. The later :link rule wins, and the hover style never shows.
The safe order is LVHA: :link, :visited, :hover, :active. Many people remember it as "LoVe HAte".
a:link {
color: #0645ad;
}
a:visited {
color: #6b3fa0;
}
a:hover {
text-decoration: underline;
}
a:active {
color: #c00;
}Each rule changes a different property where it can. Hover only adds an underline, so a visited link keeps its purple color while hovered. Only :active overrides the color, and it comes last, so it wins while the link is pressed.
Common errors
- Wrong order. Putting
:hoverabove:visitedmakes hover styles disappear on visited links. This is the most common mistake. - Styling bare
a. A rule such asa { color: blue; }placed after the state rules has lower specificity thana:link, but it still matches<a>elements withouthref. Usea:linkwhen you mean real links only. - Changing layout in
:visited. Browsers limit which properties:visitedcan change, to stop sites from detecting a user's history. You can set colors, such ascolorandbackground-color, and border and outline colors. Other properties are ignored. Do not expect size, font, orcontentchanges. - Reading visited styles from JavaScript.
getComputedStylereports the unvisited value for a visited link. This is intentional.
Keyboard users
:hover and :active mostly help mouse and touch users. A keyboard user needs a visible focus style as well. Add :focus-visible after the LVHA rules, which is a common extension of the order called LVHFA. The next chapters on focus cover it in detail.
Exercise
A stylesheet lists a:hover { color: red; } first, then a:link { color: blue; }. A user hovers over an unvisited link. What color is it, and how do you fix it?
Show answer
It is blue. Both rules match and have equal specificity, so the later a:link rule wins. Fix it by reordering the rules to :link, :visited, :hover, :active.
Exercise
Why can you not change the font-size of a link in an a:visited rule?
Show answer
Browsers restrict the properties :visited can change, mostly to colors. This prevents a page from detecting which sites a user has visited.