Reputation: 1164
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>
Upvotes: 0
Views: 74
Reputation: 3667
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'?
Upvotes: 0
Reputation: 201568
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
(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).
Upvotes: 0
Reputation: 46559
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.
Upvotes: 1