0

When linking to specific parts of pages using bookmarks, you can link to named anchors or IDs. Which is semantically correct?

 <a href="#part1">Part 1</a>
 <a name="part1">Part 1</a>

or

 <a href="#part2">Part 2</a>
 <h1 id="part2">Part 2</h1>
Devin Rodriguez
  • 1,144
  • 1
  • 13
  • 30

3 Answers3

1

In HTML 4, both are correct. In XHTML, the first one is deprecated.
Not sure about HTML5, its specs change all the time. Pretty sure you can't go wrong with the second one though.

Mr Lister
  • 45,515
  • 15
  • 108
  • 150
  • 1
    The first is deprecated in XHTML 1.1, not in 1.0. `id` is preferred though. – Quentin Jan 04 '12 at 20:24
  • Oh drat. It was XHTML 1.1 that *removed* the name attribute for links entirely. You're right that it was 1.0 that deprecated it. No need to worry about browser compatibility for `id` though: You have to go back to Netscape 4.x to find a browser that doesn't support it for that purpose. – Quentin Jan 12 '12 at 09:12
0

Always go with Part 2...

For HTML 4 and XHTML...

But for HTML5 see the question and answer below: HTML Anchors with 'name' or 'id'?

Community
  • 1
  • 1
Aaron Brewer
  • 3,567
  • 18
  • 48
  • 78
0

Both are technically correct, but the id attribute is favored in new specifications and drafts. The semantic question depends on what you are linking to—some text, some heading, or some part? It sounds most logical to link to a part, so you would use something like

Part 2

Heading for part 2

Content of part 2.

(You could use the HTML2 <section> element instead of <div>, but nobody has so far given a practical reason to do so.)

If you link to a heading instead, you can use <h2 id="part2">...</h2> or <h2><a name="part2">...</a></h2>. The latter is “classical” as it was used before HTML had the id attribute, but there is hardly a reason to use it any more—it is slightly more complicated markup, and it is slightly less logical (you want to link to a heading, not to text inside a heading, though the difference is small when it’s all the text there).

Jukka K. Korpela
  • 195,524
  • 37
  • 270
  • 390