On this page10 sections
Lecture 012: Styling Links
| Category | 3: Text, Fonts & Content |
| Lecture | 012 of 057 |
| Estimated video length | 10 min |
| Target | CSS3 · any modern browser |
| Level | Beginner |
Links need to look clickable, show when they're hovered, and stay readable. CSS styles each state of a link separately, and can turn a plain link into a button.
Learning Objectives
- Style the four link states:
:link,:visited,:hover,:active - Control underlines with
text-decoration - Turn links into buttons
- Keep links accessible
1. The Four States
Example: hover over and click the link:
<style>
a:link { color: #1971c2; } /* not visited */
a:visited { color: #7048e8; } /* visited */
a:hover { color: #e8590c; } /* mouse over */
a:active { color: #c92a2a; } /* being clicked */
</style>
<p>Read the <a href="https://example.com" target="_blank" rel="noopener">getting-started guide</a>.</p>Write them in this order, or later rules hide earlier ones. A memory trick: LoVe HAte.
2. Underlines
Example
<style>
.none a { text-decoration: none; }
.none a:hover { text-decoration: underline; }
.fancy a { text-decoration: underline; text-decoration-color: #ffa94d; text-decoration-thickness: 3px; text-underline-offset: 4px; }
</style>
<p class="none">Underline only on hover: <a href="#">Pricing</a></p>
<p class="fancy">A thick orange underline: <a href="#">Our story</a></p>3. Background Colour on Hover
Example
<style>
.menu a { padding: 6px 10px; color: #343a40; text-decoration: none; border-radius: 6px; }
.menu a:hover { background: #fff3bf; }
</style>
<p class="menu"><a href="#">Home</a> <a href="#">Courses</a> <a href="#">Contact</a></p>4. Link Buttons
Example
<style>
a.btn {
display: inline-block;
padding: 10px 20px;
background: #2f9e44;
color: white;
text-decoration: none;
border-radius: 8px;
font-weight: 600;
transition: background 0.2s;
}
a.btn:hover { background: #2b8a3e; }
a.btn:active { transform: translateY(1px); }
a.btn.outline { background: transparent; color: #2f9e44; border: 2px solid #2f9e44; }
a.btn.outline:hover { background: #ebfbee; }
</style>
<a class="btn" href="#">Start learning</a>
<a class="btn outline" href="#">See courses</a>display: inline-block lets the padding work properly on the inline <a>.
5. The Cursor
Links show a pointing hand automatically. Other clickable things can use it too:
.clickable { cursor: pointer; }Other cursor values include default, text, move, not-allowed, wait and help.
Common Beginner Mistakes
1. Wrong state order. :hover before :visited never shows on visited links.
2. Removing underlines with no other clue. Links inside paragraphs must stand out by more than colour alone.
3. Links that look like buttons but act as buttons. If it performs an action rather than going somewhere, use <button>.
Practice Questions
- Name the four link states in the correct order.
- Remove the underline from links, but show it on hover.
- Which property sets the colour of an underline?
- Why add
display: inline-blockto a link button? - Which cursor value shows a pointing hand?
Practice Answers
:link,:visited,:hover,:active.a { text-decoration: none; } a:hover { text-decoration: underline; }text-decoration-color- So the padding and size work like a box, not just around the text line.
cursor: pointer;
Key Takeaways
- Style
:link,:visited,:hover,:active, in that order. text-decorationcontrols underlines (colour, thickness, offset).display: inline-block+ padding + background makes link buttons.- Keep links clearly recognisable.