Key takeaways:
Understanding the various link states—default (a
), visited (a:visited
), hover (a:hover
), active (a:active
), and focus (a:focus
)—allows you to create responsive navigation elements.
Proper ordering of CSS pseudo-classes—default, visited, hover, focus, active—ensures that styles are applied correctly without unintended overrides.
Implementing clear and visible focus styles for links is essential for users who rely on keyboard navigation or assistive technologies.
CSS provides the tools necessary to style HTML elements, including links, which are among the most interactive components on a webpage. Properly styled links not only guide users through your content but also enhance the overall aesthetic appeal of your site. In this answer, we will look at different ways that we can style links using CSS.
Understanding link states
Links can exist in various states based on user interaction and browser history. CSS pseudo-classesCSS pseudo-class selectors are predefined keywords that define the state of an element or target a child element. They occur pretty frequently, and we can see them in action when a colon follows an element. allow you to target these specific states to apply distinct styles, enhancing functionality and user experience. Links have some states that you can style using CSS pseudo-classes:
Default (a
): Unvisited link.
Visited (a:visited
): After a link has been visited.
Hover (a:hover
): When a user hovers over the link.
Active (a:active
): The moment the link is clicked.
Focus (a:focus
): A link that is focused.
Let's discuss each state in detail with examples.
1. The a
style
The a
pseudo-class denotes a link that has not been visited yet. This is the default state of a link before any user interaction. It targets all unvisited links on a web page.
In the example below, all unvisited links are displayed in red without underlines, providing a clean and modern look.