CSS Links
In this page:
a:link { property: value; }
a:visited { property: value; }
a:hover { property: value; }
a:active { property: value; }
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.
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.
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.
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.
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.
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: