CSS Anchor States

Theory & Explanation

Anchor elements (<a>) are used to create hyperlinks in HTML. CSS provides pseudo-classes to style links based on their state, improving usability and accessibility. The five main anchor states are:

Order matters: Always use the LVHAF order in your CSS: :link, :visited, :hover, :active, :focus. This ensures all states are styled as expected.

How Anchor States Work

Accessibility & Best Practices

Code Examples

/* Basic anchor states */
a:link {
  color: #3498db;
}
a:visited {
  color: #8e44ad;
}
a:hover, a:focus {
  color: #fff;
  background: #3498db;
  text-decoration: underline;
}
a:active {
  color: #ffd200;
}

/* Custom button-style link */
.button-link:link, .button-link:visited {
  display: inline-block;
  background: linear-gradient(90deg, #3498db 60%, #8e44ad 100%);
  color: #fff;
  padding: 8px 20px;
  border-radius: 6px;
  font-weight: 600;
  text-decoration: none;
  transition: background 0.2s, color 0.2s, box-shadow 0.2s;
}
.button-link:hover, .button-link:focus {
  background: linear-gradient(90deg, #8e44ad 0%, #3498db 100%);
  color: #ffd200;
  box-shadow: 0 4px 16px 0 #8e44ad44;
  text-decoration: underline;
}
.button-link:active {
  background: #23272e;
  color: #ffd200;
}

Live Demos

Try clicking, hovering, tabbing, and visiting the links above to see the different anchor states in action. Button-style links and navigation links demonstrate advanced styling and accessibility.

← Back to Home