The HTML code <a name="some_bookmark">text</a>
is very useful for creating links to specific sections of a page (e.g., page.html#some_bookmark
). However, the W3C spec now marks the name
attribute of the a
tag as "obsolete."
If this is the case, then what is preferred? Is there a new <bookmark>
tag or similar?
You can place an id=""
attribute on any element and it will have the same effect.
These are typically placed on heading elements.
Have a look at the HTML5 spec.
At Obsolete features you’ll find:
Authors should not specify the name
attribute on a
elements.
When clicking on name
, you’ll find:
[The following attributes are obsolete (though the elements are still part of the language), and must not be used by authors:]
name
on a
elements (except as noted in the previous section)
name
on embed
elements
name
on img
elements
name
on option
elements
→ Use the id
attribute instead.
Click on id
. You’ll see that id
is a global attribute, this means that it can be used on any element.
The id
attribute specifies its element's unique identifier (ID).
[…]
Note: An element's unique identifier can be used for a variety of purposes, most notably as a way to link to specific parts of a document using fragment identifiers, as a way to target an element when scripting, and as a way to style a specific element from CSS.
you should use id=""
attribute. :)
(Note: migrated from https://webmasters.stackexchange.com/a/30031/10884 and edited for brevity.)
If you need to jump users to in-page links, also known as fragment identifiers, you can set the id
attribute (which is used for more than just frag ids) on any element. Then use the usual #
in the URL of a href
attribute of an a
element. Here’s an example:
<body>
<p>Despite the many
<a href="#benefits-of-gum-chewing">benefits</a>
you may experience while chewing gum, there are also many drawbacks,
especially with
<a href="http://www.example.org/sugar.html#cons">non-sugarless</a>
gum.</p>
...
<section id="benefits-of-gum-chewing">
<h1>Benefits of Gum Chewing</h1>
...
</section>
</body>
When writing my own pages, I like to give an id
to each <section>
tag (HTML5), even if I don’t plan on using it. The value of the id
is a URL-friendly version of its heading’s content. You can achieve the same effect by assigning the same id
to an <h1>
, etc.
Using id is not always ideal as they are unique, for example if you have a list of links as menu items and you want to perform some javascript on only certain ones of them when the user hovers over, using id's would be a mess.
Currently the best way I believe is using a class to identify them as a group.So something like this:
<a class="mylink">Menu Item one</a>
<a class="mylink">Menu Item two</a>
<a class="mylink">Menu Item three</a>
<a>Menu Item four</a>
But I've just kept on using the name tag, its stupid to deprecate something this minor as it has no performance effect or anything else, just causes problems if browsers decide not to accept the name tag at one moment. It strays us away from using standards as well.