CSS Links
In this page:
Link States: hover, visited, active, focus
CSS pseudo-classes let you target a link based on its current state: :hover while a mouse sits over it, :visited once a visitor has clicked it before, :active during the exact moment of clicking, and :focus when it's selected via keyboard.
Note: Even small state changes, like a slightly different color on hover, give visitors confidence that a page is responding to them.
Warning: Browsers restrict how much you can style :visited links (mainly just color) for privacy reasons, to prevent sites from detecting browsing history.
Example: Link States: hover, visited, active, focus
<style>
a:hover { color: red; }
a:visited { color: purple; }
a:active { color: orange; }
a:focus { outline: 2px solid blue; }
</style>
<a href="#">Interactive link</a>
The Correct Pseudo-Class Order
Because CSS applies later rules over earlier ones with equal specificity, link pseudo-classes need to be written in a specific order to all work correctly together: :link, :visited, :hover, then :active.
Note: Remember the order with the mnemonic 'LoVe HAte': link, visited, hover, active.
Warning: Writing :hover before :visited can cause the hover style to never actually show on a visited link, since :visited would override it further down.
Example: The Correct Pseudo-Class Order
<style>
a:link { color: blue; }
a:visited { color: purple; }
a:hover { color: red; }
a:active { color: orange; }
</style>
<a href="#">LVHA order: link, visited, hover, active</a>
Underline on Hover
A common, subtle pattern is to keep links free of an underline by default, then reveal one only on hover, giving a satisfying bit of interactivity without cluttering ordinary reading.
Note: This pattern works especially well for navigation links, where a clean look matters but hover feedback is still expected.
Warning: If you remove the default underline entirely and skip a hover state too, links can become genuinely difficult to distinguish from plain text.
Example: Underline on Hover
<style>
a {
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
</style>
<a href="#">Underline appears only on hover</a>
Styling Links as Buttons
A link doesn't have to look like plain text, adding background, padding, and border-radius can make it look and feel like a real button, while it still behaves like a normal link underneath.
Note: Button-styled links are great for primary calls to action, like 'Get Started' or Download, where you want them to stand out visually.
Warning: A link styled to look like a button should still behave like a link (navigate somewhere with href), don't disguise a button-triggered action as a link.
Example: Styling Links as Buttons
<style>
a.button {
background: royalblue;
color: white;
padding: 10px 20px;
border-radius: 6px;
}
</style>
<a href="#" class="button">Get Started</a>
Accessible Focus Styles on Links
Just like buttons and inputs, links need a clear visible style when focused via keyboard, so someone tabbing through the page can always tell exactly which link is currently selected.
Note: Design a link's :focus style to be at least as visible as its :hover style, keyboard users deserve equally clear feedback.
Warning: Removing outline on links without providing a custom :focus style leaves keyboard users with no way to see which link is currently selected.
Example: Accessible Focus Styles on Links
<style>
a:focus {
outline: 3px solid orange;
outline-offset: 2px;
}
</style>
<a href="#">Tab to me to see a clear focus style</a>
- Writing link pseudo-class rules in the wrong order, which can cause later states to never actually apply.
- Styling :hover but forgetting :focus, leaving keyboard users without the same visual feedback mouse users get.
- Removing all visual distinction from links, making it hard for visitors to tell what's clickable at a glance.
- Links have distinct states: :link, :visited, :hover, :active, and :focus, each styleable on its own.
- A common memory aid for pseudo-class order is 'LoVe HAte': link, visited, hover, active.
- Links can be styled to look like buttons using background, padding, and border-radius, not just as plain underlined text.
All standard link pseudo-classes covered here are supported by every modern browser.
Chapter Quiz — Complete all 10 topics to unlock
0/10 topics done
Complete these topics first: