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:
<a> elements with an href that haven't been visited.Order matters: Always use the LVHAF order in your CSS: :link, :visited, :hover, :active, :focus. This ensures all states are styled as expected.
:link until the user clicks them.:visited after the user visits the URL (if allowed by browser privacy).:visited styling to color, background-color, border-color, and outline-color for privacy.:focus style for keyboard users.:hover and :focus together for consistent experience.:visited to help users distinguish visited links, but avoid using sensitive color cues.<a> elements for navigation and actions that change the page or context./* 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;
}
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.