CSS Basic
CSS :visited Selector
Photo Credit to CodeToFun
🙋 Introduction
The :visited
selector in CSS is used to target and style links that have already been visited by the user. It is a pseudo-class that helps differentiate between visited and unvisited links, improving navigation and usability on websites. By applying specific styles to visited links, you can help users track their browsing history more easily.
💡 Syntax
The signature of the :visited
Selector is as follows:
:visited {
/* CSS properties */
}
The :visited
pseudo-class is used specifically with anchor (<a>
) elements that have an href
attribute.
📝 Example
Here’s an example of how to use the :visited
selector in CSS:
☠️ HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>CSS :visited Selector Example</title>
<link rel="stylesheet" href="styles.css">
</head>
<body>
<h1>CSS :visited Selector Example</h1>
<a href="https://www.example.com">Visit Example.com</a><br>
<a href="https://www.openai.com">Visit OpenAI.com</a>
</body>
</html>
🎨 CSS
/* Style for unvisited links */
a {
color: blue;
text-decoration: none;
}
/* Style for visited links */
a:visited {
color: purple;
text-decoration: underline;
}
In this example:
- Unvisited links are styled with a blue color and no underline.
- Visited links turn purple and have an underline for distinction.
💬 Usage Tips
- The
:visited
pseudo-class only applies to anchor (<a>
) elements with a validhref
attribute. It does not affect other elements. - Due to privacy concerns, modern browsers limit the styles you can apply to
:visited
. You can only change certain properties likecolor
,background-color
,border-color
, andoutline-color
.
⚠️ Common Pitfalls
- Limited Styling Properties: Only specific properties can be applied to
:visited
links due to browser security measures. Properties likecontent
,transform
, orvisibility
cannot be altered for:visited
links to prevent privacy leaks. - Caching Issues: Sometimes, the browser’s cache may cause unexpected behavior with
:visited
links, where a link appears visited when it hasn’t been recently clicked. Clearing the browser cache can solve this issue.
🎉 Conclusion
The :visited
selector is a simple yet effective way to style visited links, helping users distinguish between pages they have already explored and those they haven’t. While browser security measures limit the properties that can be applied to visited links, the selector still plays an important role in enhancing website navigation and user experience.
👨💻 Join our Community:
Author
For over eight years, I worked as a full-stack web developer. Now, I have chosen my profession as a full-time blogger at codetofun.com.
Buy me a coffee to make codetofun.com free for everyone.
Buy me a Coffee
If you have any doubts regarding this article (CSS :visited Selector), please comment here. I will help you immediately.