:any-link
is a new pseudo-class proposed in Selectors level 4, that matches all elements that would be matched by :link, :visited
. From what I see, its main purpose is to simplify selectors that need to select any hyperlinks, since the naming of :link
is misleading; it specifically means unvisited links only, rather than all hyperlinks (which makes it essentially the opposite of :visited
).
For the purposes of :link
and :visited
, WHATWG HTML and W3C HTML5 both define a "hyperlink" as any one of:
An <a>
element that has an href
attribute. This excludes named anchors (that is, <a>
elements without an href
attribute but instead with a name
attribute), which were used traditionally to mark anchors in a page, now superseded by the use of an id
attribute on any element. See named anchors in HTML 4.
An <area>
element that has an href
attribute.
A <link>
element that has an href
attribute.
For example, consider a scenario where links in the page header should be colored differently from all other links:
body > header > a:link, body > header > a:visited {
color: white;
}
Notice the body > header
part is duplicated across both selectors. This seems redundant, but is currently necessary in order to color links in the page header differently from the rest, but regardless of their visited state. This is because body > header > a
is not specific enough which requires using !important
to override anyway, and body > header > a:link
troublesomely only applies to unvisited links.
With the :any-link
pseudo-class, you can simply do this instead:
body > header > a:any-link {
color: white;
}
Specificity is exactly the same as with each individual half, so there should be no issues there.
Of course, since no browser implements it unprefixed yet, that won't work. As an alternative, considering you're most likely working with an HTML document anyway you can just use a[href]
instead, which works in all browsers including IE7+ on top of also being equally specific:
body > header > a[href] {
color: white;
}
There's a much more elaborate explanation regarding the use of a
versus a:link, a:visited
versus a:any-link
versus a[href]
in this other answer of mine.
Like anything else that has a prefix in CSS, :-moz-any-link
and :-webkit-any-link
exist only for experimental purposes, so you shouldn't use them with your sites. Besides, even if you were to use them right now, you'd have to duplicate the rules themselves (and not just the selectors!) since browsers have to drop entire rules on unrecognized selectors, rendering them pretty useless in real-world code!
As of early 2013, no other implementations of :any-link
exist that I know of. I'm unsure as well as to whether this was implemented by the respective vendors and then proposed for inclusion in Selectors 4, or if it was preliminarily specced before the vendors began implementing it, but I don't think that matters.
Speaking of which, be sure not to confuse the :-moz-any-link
/:-webkit-any-link
pseudo-class with :-moz-any()
/:-webkit-any()
, the latter of which is specced as :matches()
instead (possibly to avoid naming confusion?).