6

The Issue

I have read a couple older SO posts researching info on the anchor pseudo classes, and keep coming across confusion between "a" vs "a:link" and when and why you would use either. In the most common reason I've seen it is often stated that "a" would style links like

<a name="something">

My Questions

  1. I'm just curious if anyone can explain WHY you would want to do something like that?
  2. I've read that maybe it has something to with JavaScript targeting, but with HTML5/CSS3 and libraries like jQuery is this even a valid technique to use anymore?
  3. In what instances would using an anchor tag that is not a link (i.e., doesn't have an "href" attribute) be #BestPractice, or is this method completely deprecated?
  • It's a *target* for other links. – nnnnnn Aug 10 '16 at 22:34
  • That was my first thought but the anchor still uses `href` – zer00ne Aug 10 '16 at 22:40
  • 1
    Good question. Answer: never. Often I see this when people should be using ` – David Gilbertson Aug 10 '16 at 22:56

2 Answers2

4

That can be used for in-page targeting of elements (e.g. to scroll to a certain point):

<a name="table-of-contents"></a>
<h1>Table of Contents</h1>
...
<a href="#table-of-contents">Table of Contents</a>

Though, this is often redundant (and may also take up white space) because elements with IDs can be targeted directly:

<h1 id="table-of-contents">Table of Contents</h1>
...
<a href="#table-of-contents">Table of Contents</a>
Ates Goral
  • 137,716
  • 26
  • 137
  • 190
  • A link is also focusable by default. Makes it more friendly for accessibility if you need a user to tab to thr element in the default order. – user3366016 Aug 10 '16 at 22:45
  • 1
    @user3366016 - But you can't tab to an anchor that doesn't have an href, can you? I don't think they're focusable. – nnnnnn Aug 10 '16 at 22:47
  • 1
    @nnnnnn good catch, I apparently always add href="#" or similar in these cases. This [post](http://stackoverflow.com/a/19167175/3366016) has some good input on the topic though. It mentions how the a tag without href attribute is a placeholder link and may be used in areas such as breadcrumbs or when the href is added dynamically. – user3366016 Aug 11 '16 at 00:13
  • 1
    @Ates Goral Perfect answer - I accepted it as correct! For those researching this, I found this link on (https://www.w3.org/TR/html-markup/a.html#a-examples) explaining that the old "name" version is officially obsolete, by their standards anyway. – Eric Hepperle - CodeSlayer2010 Aug 11 '16 at 02:16
1

The <a> name attribute is technically no longer supported in HTML5, although browsers still support it for backwards compatibility.

https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#Obsolete

I recommend you stick with <a id="something"> from here on out. If you've seen examples that use name, then they're provably still residual from HTML 4 days.

M -
  • 26,908
  • 11
  • 49
  • 81