Understanding CSS Pseudo-Classes: :link, :visited, :active and :hover
CSS pseudo-classes are essential for applying styles based on the state or interaction of elements. They provide dynamic styling capabilities, especially for links. Here’s a detailed overview of how to use these pseudo-classes:
1. :hover
The :hover
pseudo-class styles a link when the user’s cursor is hovering over it. This is useful for providing interactive feedback and enhancing user experience.
Example:
a:hover {
color: orange; /* Color of links when hovered over */
text-decoration: underline; /* Underline links on hover */
}
This rule changes the link color to orange and adds an underline when the user hovers over it, making the link appear interactive.
2. :link
The :link
pseudo-class targets hyperlinks that have not yet been visited by the user. It sets the initial appearance of these links.
Example:
a:link {
color: blue; /* Color of unvisited links */
}
This rule ensures that unvisited links appear blue, making them easily recognizable to users.
3. :visited
The :visited
pseudo-class applies styles to links that the user has previously visited. This helps in distinguishing between links that have been clicked and those that have not.
Example:
a:visited {
color: purple; /* Color of visited links */
}
This rule changes the color of visited links to purple, providing visual feedback on the user’s browsing history.
4. :active
The :active
pseudo-class styles a link while it is being clicked by the user. It provides immediate feedback during the click action.
Example:
a:active {
color: red; /* Color of a link while being clicked */
}
This rule changes the link color to red during the click, giving clear feedback that the link is being activated.
Combining Pseudo-Classes
For a complete styling experience, combine these pseudo-classes in your CSS. The order in which they appear is important because it determines their priority:
a:link {
color: blue; /* Unvisited link */
}
a:visited {
color: purple; /* Visited link */
}
a:hover {
color: orange; /* Link when hovered over */
text-decoration: underline; /* Add underline on hover */
}
a:active {
color: red; /* Link while being clicked */
}
:link
: Applies to unvisited links.:visited
: Applies to links that have been visited.:hover
: Applies when the user hovers over the link.:active
: Applies when the link is being clicked.
Final Code
Here’s a complete HTML and CSS example that demonstrates the use of these pseudo-classes:
Summary
CSS pseudo-classes like :hover
, :link
, :visited
, and :active
enhance the styling of links based on their state and user interactions. Using these pseudo-classes effectively provides clear visual feedback and improves the overall user experience on your website.
Thanks for reading all the way to the end! 💖
If you have any questions, please use the comments section 💬 Let's connect! Find me on the web 🔗
If you have any Queries or Suggestions, feel free to reach out to me.
Happy Coding :)❤️
Subscribe to my newsletter
Read articles from Kushal Das directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Kushal Das
Kushal Das
A Full Stack Web Developer | A Mentor | A freelancer💻 | Data science enthusiastic | Open source enthusiastic | Create and write content | Enjoy learning new techs | love meeting new people! 😊