:visited Back
:visitedis a pseudo-class used to select and style visited links in a page.- Note that it will only select anchors
<a>that have an href attribute. - The
:visitedpseudo-class applies once the link has been visited by the user.
<!-- will select any of these -->
<a href="#">Random Link</a>
<a href="#id">Internal Link</a>
<a href="http://codrops.com">External Link</a>
<!-- will not select this -->
<a>No href attribute</a>
Case: learning :visited
See the Pen NNyLNB by aleen42 (@aleen42) on CodePen.
Note
- An element can be both
:visitedand:active(or:linkand:active). - When the four link styling pseudo-classes are used, they are preferably used in the following order:
:link,:visited,:hover, and:active. For example:
a:link {
/** style links */
}
a:visited {
/** style visited links */
}
a:focus {
/** style focus state links */
}
a:hover {
/** style hover links */
}
a:active {
/** style active state links */
}
- When you're styling links using
:focusit is recommended that you provide the:focusstyles after:linkand:visitedstyles, otherwise the:focusstyles would be overridden by those of:linkand:visited. - The
:visitedpseudo-class can, along with some scripting, be used by websites to attack and "sniff" a user's web browsing history. In order to prevent privacy issues caused by this, modern browsers have set limitations on the kind of styles that can be applied to:visitedlinks. These limitations help protect a user's privacy by preventing scripts from being able to identify and retrieve links that have been visited from a web page. The solution to this privacy issue was proposed by Mozilla's David Baron. - Baron's solution limits the CSS properties that can be used to style visited links to color, background-color, border-*-color, outline-color and, column-rule-color.
- This means that the above properties are the only properties you can use to style
:visitedlinks that would actually work. - There's also an "anomaly" related to the background-color applied to a link using
:visited: the background color in the:visitedstate won't be applied to the link unless an actual "real" background color is applied to the link prior to its visited state - that is, in its:linkstate. - For example, the following will not apply a background color to the visited link:
a:link {
color: white;
background-color: transparent;
/** OR */
/** if no background color is set at all */
}
a:visited {
color: white;
background-color: black;
}
- While this will set the background color on the visited links:
a:link {
color: white;
background-color: #eee;
}
a:visited {
color: white;
background-color: black;
}
