Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do we need "a:link"? Why not just "a"?

Tags:

It seems the following variants produce identical results:

/* 1 */ a, a:visited, a:active { color: black; } a:hover { color: red; }  /* 2 */ a, a:link, a:visited, a:active { color: black; } a:hover { color: red; }  /* 3 */ a:link, a:visited, a:active { color: black; } a:hover { color: red; } 

Most guidance on the web uses 2 or 3. Why not go for the simplest variant which is 1? I cannot find a good reason to ever apply :link if all you need is one style for normal links and one for hover.

Is it best-practice to not use :link? Why or why not?

I don't care whether the link is visited or not. Both receive the same style. I only care about hover or not hover. This question is not about what the variants do - all do the same thing. It is about what the best variant is. Is one of the variants faulty? Which one is best-practice?

like image 828
usr Avatar asked Aug 24 '13 17:08

usr


2 Answers

Because not every a is a link.

<a name="table_of_contents">Table of Contents</a> 

isn't a link, it's an anchor that can be linked to with <a href="#table_of_contents">.

a will match it, a:link won't.

like image 113
hobbs Avatar answered Oct 01 '22 14:10

hobbs


It is used to differentiate between simple anchors and anchors with href attributes. See demo jsfiddle here.

<style> a { color: red; } a:link { color: blue; } </style> <a name="anchor">No href</a><br /> <a href="http://stackoverflow.com/">With href</a> 

EDIT: For this reason, it is important to cover all cases in your CSS. Option 2 is the only option that completely covers all cases. If you do not have anchor elements without href attributes, you are safe with option 1.

like image 28
Everett Green Avatar answered Oct 01 '22 13:10

Everett Green