Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the :any-link pseudo-class for?

I don't know if it's a part of any standard, but at least two major browsers have implemented it:

  • :-webkit-any-link in Chrome
  • :-moz-any-link in Firefox

I can't find any documentation for it. I would like to know its purpose, browser support, and examples of usage.

like image 709
Pavlo Avatar asked Jan 21 '13 10:01

Pavlo


People also ask

What does the pseudo-class link do?

The :link CSS pseudo-class represents an element that has not yet been visited. It matches every unvisited <a> or <area> element that has an href attribute.

What is the use of pseudo?

Pseudo means not authentic, false, pretend. Pseudo may also mean having a close resemblance to. The word pseudo is often used with another word, hyphenated, to refer to something that is inauthentic, such as pseudo-science or pseudo-intellectual. Pseudo- is also a prefix used in words such as pseudonym.

What is the meaning of pseudo-class?

What is a pseudo-class? A pseudo-class is a selector that selects elements that are in a specific state, e.g. they are the first element of their type, or they are being hovered over by the mouse pointer.


1 Answers

: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?).

like image 189
BoltClock Avatar answered Oct 20 '22 08:10

BoltClock