Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Should I make HTML Anchors with 'name' or 'id'?

When one wants to refer to some part of a webpage with the "http://example.com/#foo" method, should one use

<h1><a name="foo"/>Foo Title</h1> 

or

<h1 id="foo">Foo Title</h1> 

They both work, but are they equal, or do they have semantic differences?

like image 728
Henrik Paul Avatar asked Jan 27 '09 18:01

Henrik Paul


People also ask

Can we put ID for anchor tag HTML?

IDs for anchor tags are completely optional, although depending on the documentation you see, it may seem as though it's a standard in web design or you absolutely can't do click tracking without it.

Which is correct for anchor tag?

The <a> tag defines a hyperlink, which is used to link from one page to another. The most important attribute of the <a> element is the href attribute, which indicates the link's destination. By default, links will appear as follows in all browsers: An unvisited link is underlined and blue.

Which is the correct HTML code for inserting a name and anchor?

Anchor Tag - HTML <a> Tag The <a> tag is used to create an anchor to link from, the href attribute is used to address the document to link to, and the words between the open and close of the anchor tag will be displayed as a hyperlink.


1 Answers

According to the HTML 5 specification, 5.9.8 Navigating to a fragment identifier:

For HTML documents (and the text/html MIME type), the following processing model must be followed to determine what the indicated part of the document is.

  1. Parse the URL, and let fragid be the <fragment> component of the URL.
  2. If fragid is the empty string, then the indicated part of the document is the top of the document.
  3. If there is an element in the DOM that has an ID exactly equal to fragid, then the first such element in tree order is the indicated part of the document; stop the algorithm here.
  4. If there is an a element in the DOM that has a name attribute whose value is exactly equal to fragid, then the first such element in tree order is the indicated part of the document; stop the algorithm here.
  5. Otherwise, there is no indicated part of the document.

So, it will look for id="foo", and then will follow to name="foo"

Edit: As pointed out by @hsivonen, in HTML5 the a element has no name attribute. However, the above rules still apply to other named elements.

like image 63
Greg Avatar answered Oct 02 '22 14:10

Greg