Devin Rodriguez
Devin Rodriguez

Reputation: 1164

What is semantically correct when using page bookmarks?

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

Answers (3)

Aaron Brewer
Aaron Brewer

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

Jukka K. Korpela
Jukka K. Korpela

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

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).

Upvotes: 0

Mr Lister
Mr Lister

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

Related Questions