CSS Links
Links are the heart of the web, and CSS lets you style them like any other text — colour, font, background, decoration. What makes links special is that they have states: unvisited, visited, hovered, focused and being clicked.
Link states
| Selector | When it applies |
|---|---|
a:link |
A link that hasn't been visited |
a:visited |
A link whose URL is in the browser history |
a:hover |
The mouse is over the link |
a:focus / a:focus-visible |
The link has keyboard focus |
a:active |
The moment the link is being clicked |
When you style several states, order matters because they have equal specificity and later rules win. Use L-V-H-A: :link, :visited, :hover, :active ("LoVe HAte").
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; font-size: 20px; padding: 20px; }
a:link { color: #1565c0; }
a:visited { color: #6a1b9a; }
a:hover { color: #e65100; background: #fff3e0; }
a:active { color: #d50000; }
a:focus-visible { outline: 3px solid #ffab00; outline-offset: 2px; }
</style>
</head>
<body>
<p>Hover over <a href="#one">this link</a>, then press and hold the mouse on it.</p>
<p>Press <b>Tab</b> to see <a href="#two">the focus style</a>.</p>
</body>
</html>Note: For privacy reasons,
:visitedmay only change colours — not size, background images or anything that could reveal history to scripts.
Text decoration on links
Links are underlined by default. You can remove it and bring it back on hover, or make a nicer underline with text-underline-offset and text-decoration-thickness.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; font-size: 20px; padding: 16px; }
.a1 { text-decoration: none; color: #00796b; }
.a1:hover { text-decoration: underline; }
.a2 { color: #c2185b; text-decoration-thickness: 3px; text-underline-offset: 6px; text-decoration-color: #f8bbd0; }
.a2:hover { text-decoration-color: #c2185b; }
.a3 { color: #283593; text-decoration: none; background: linear-gradient(#283593, #283593) no-repeat left bottom / 0 3px; transition: background-size .3s; padding-bottom: 3px; }
.a3:hover { background-size: 100% 3px; }
</style>
</head>
<body>
<p><a class="a1" href="#">Underline on hover</a></p>
<p><a class="a2" href="#">Soft thick underline</a></p>
<p><a class="a3" href="#">Animated underline</a></p>
</body>
</html>Link buttons
A link that should look like a button needs display: inline-block (so padding takes space), a background, padding and rounded corners. Add a hover state so users know it's interactive.
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; padding: 20px; }
.btn {
display: inline-block;
padding: 12px 22px;
margin-right: 10px;
border-radius: 8px;
font-weight: 600;
text-decoration: none;
transition: background .2s, transform .1s;
}
.primary { background: #2962ff; color: white; }
.primary:hover { background: #0039cb; }
.ghost { color: #2962ff; border: 2px solid #2962ff; }
.ghost:hover { background: #e3eaff; }
.btn:active { transform: translateY(2px); }
</style>
</head>
<body>
<a class="btn primary" href="#">Sign up</a>
<a class="btn ghost" href="#">Learn more</a>
</body>
</html>Cursors
The cursor property changes the mouse pointer. Links get pointer automatically; other clickable elements should too.
| Value | Typical use |
|---|---|
pointer |
Clickable things |
default |
Normal arrow |
text |
Selectable text |
not-allowed |
Disabled controls |
help |
Extra info available |
wait / progress |
Busy |
grab / grabbing |
Draggable |
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: system-ui, sans-serif; display: flex; flex-wrap: wrap; gap: 10px; padding: 10px; }
div { padding: 16px; background: #eceff1; border-radius: 8px; }
</style>
</head>
<body>
<div style="cursor: pointer">pointer</div>
<div style="cursor: not-allowed">not-allowed</div>
<div style="cursor: help">help</div>
<div style="cursor: wait">wait</div>
<div style="cursor: grab">grab</div>
<div style="cursor: crosshair">crosshair</div>
</body>
</html>Tip: Make sure links stand out from surrounding text by more than colour alone (underline, weight or an icon) — some users can't distinguish colours.
Test yourself
In what order should link state rules be written?
- :hover, :link, :active, :visited
- :link, :visited, :hover, :active
- :active, :hover, :visited, :link
- Order doesn't matter
Why does a link styled as a button usually need display: inline-block?
- To make it clickable
- So vertical padding takes up layout space
- To remove the underline
- To enable :hover
Which properties may :visited change?
- Any property
- Only colour-related properties
- Only font-size
- Only display