The :visited
pseudo-class applies styles to an <a>
element that links to a URL the user has already visited. For privacy reasons, the properties that can be changed with this pseudo-class are limited.
Example 1: Basic Visited Link
/* Style for a link */
.my-link {
color: #007bff;
}
/* Change the color of the link if it has been visited */
.my-link:visited {
color: #6c757d;
}
Explanation
This is the most common use case for :visited
. It changes the color of a link after the user has navigated to it, providing a helpful visual cue of their Browse history on the site.
Example 2: Visited Link with Background Color
/* Style for a link */
.my-link {
color: #007bff;
}
/* Change the background color of a visited link */
.my-link:visited {
background-color: #f8f9fa;
}
Explanation
You can also change the background-color
of a visited link. This can be used to subtly highlight links that have already been explored by the user.
Example 3: Styling the Outline of a Visited Link
/* Style for a link */
.my-link {
outline: 1px solid #007bff;
}
/* Change the outline color of a visited link */
.my-link:visited {
outline-color: #6c757d;
}
Explanation
The outline-color
is another property that can be safely modified for visited links, offering another way to visually distinguish them.
Example 4: Using border-color
on Visited Links
/* Style for a link with a border */
.my-link {
border-bottom: 2px solid #007bff;
padding-bottom: 2px;
}
/* Change the border color of a visited link */
.my-link:visited {
border-bottom-color: #6c757d;
}
Explanation
If your link design includes borders, you can change their color for visited links. This allows for more integrated visual feedback within your site's design.
Example 5: Combining with Other Pseudo-classes
/* Default link style */
a {
color: #007bff;
}
/* Visited link style */
a:visited {
color: #6c757d;
}
/* Hover style for unvisited links */
a:hover {
color: #0056b3;
}
/* Hover style for visited links */
a:visited:hover {
color: #5a6268;
}
Explanation
This example shows how to provide different hover colors for visited and unvisited links, creating a more nuanced and user-friendly navigation experience.