Styling Links and Link States in CSS
Default Link Styles
Browsers apply default styles to links that most users recognize:
- Unvisited links: Blue with an underline
- Visited links: Purple with an underline
- Active links (while clicking): Red
These defaults are functional but rarely match your website's design. CSS gives you full control over how links look and behave.
Link Pseudo-Classes (LVHA Order)
CSS provides four pseudo-classes for different link states. They should be defined in the LVHA order to work correctly:
/* L - Link: unvisited links */
a:link { color: #3498db; }
/* V - Visited: already visited links */
a:visited { color: #8e44ad; }
/* H - Hover: mouse is over the link */
a:hover { color: #2980b9; }
/* A - Active: link is being clicked */
a:active { color: #e74c3c; }Why Order Matters
The LVHA order is important because of CSS specificity. All four selectors have equal specificity, so the last one defined wins when multiple states apply simultaneously. A link being hovered is also still a :link (or :visited), so :hover must come after to override them.
A common mnemonic to remember the order: LoVe HAte.
Styling All Links at Once
Most of the time, you'll style all link states together and then override specific states:
a {
color: #3498db;
text-decoration: none;
transition: color 0.2s ease;
}
a:hover {
color: #2980b9;
text-decoration: underline;
}
a:active {
color: #1a5276;
}Using a without a pseudo-class targets all link states at once.
Removing Underlines
The most common link style change is removing the default underline:
a {
text-decoration: none;
}
/* Add underline only on hover */
a:hover {
text-decoration: underline;
}Custom Underlines
For more control over underlines, use text-decoration properties or border-bottom:
/* Custom underline color and style */
a {
text-decoration: underline;
text-decoration-color: #3498db;
text-decoration-thickness: 2px;
text-underline-offset: 3px;
}
/* Using border-bottom for more control */
a {
text-decoration: none;
border-bottom: 2px solid #3498db;
padding-bottom: 2px;
}
a:hover {
border-bottom-color: #e74c3c;
}Focus Styles for Accessibility
The :focus and :focus-visible pseudo-classes are critical for keyboard navigation:
a:focus {
outline: 2px solid #3498db;
outline-offset: 2px;
}
/* Only show focus ring for keyboard users */
a:focus-visible {
outline: 2px solid #3498db;
outline-offset: 2px;
}
a:focus:not(:focus-visible) {
outline: none;
}Never remove focus styles without providing an alternative. Users who navigate with keyboards rely on visible focus indicators.
Practical Link Styles
Subtle Hover Effect
a {
color: #3498db;
text-decoration: none;
border-bottom: 1px solid transparent;
transition: border-color 0.3s ease;
}
a:hover {
border-bottom-color: #3498db;
}Color Transition
a {
color: #333;
text-decoration: none;
transition: color 0.2s ease;
}
a:hover {
color: #3498db;
}Animated Underline
a {
color: #333;
text-decoration: none;
position: relative;
}
a::after {
content: '';
position: absolute;
bottom: -2px;
left: 0;
width: 0;
height: 2px;
background: #3498db;
transition: width 0.3s ease;
}
a:hover::after {
width: 100%;
}Button-Style Links
Transform links to look and behave like buttons:
.btn-link {
display: inline-block;
padding: 0.75rem 1.5rem;
background: #3498db;
color: white;
text-decoration: none;
border-radius: 4px;
font-weight: 600;
transition: background 0.2s ease, transform 0.1s ease;
}
.btn-link:hover {
background: #2980b9;
transform: translateY(-1px);
}
.btn-link:active {
background: #1a6fa0;
transform: translateY(0);
}Styling Visited Links
For privacy reasons, browsers limit which properties you can change on :visited links. You can only change:
- color
- background-color
- border-color
- outline-color
a:visited {
color: #8e44ad; /* This works */
/* font-size: 20px; -- This will be IGNORED */
}Styling Links by Context
Different sections of your page may need different link styles:
/* Navigation links */
.nav a {
color: white;
text-decoration: none;
font-weight: 600;
}
/* Content links */
.content a {
color: #3498db;
text-decoration: underline;
}
/* Footer links */
.footer a {
color: #aaa;
text-decoration: none;
}
.footer a:hover {
color: white;
}External Link Indicators
Visually indicate links that go to external websites:
a[href^="http"]:not([href*="yourdomain.com"])::after {
content: ' \2197'; /* Arrow symbol */
font-size: 0.8em;
}Summary
CSS link styling revolves around the four pseudo-classes :link, :visited, :hover, and :active (remember LVHA order). Always include :focus styles for keyboard accessibility. Use text-decoration properties for custom underlines, transitions for smooth hover effects, and pseudo-elements for animated underlines. Style links differently based on their context (navigation, content, footer) for a polished user experience.