Don't Name Anchor Elements? ~ Fri, 29 Oct 2010 14:26:49 +0000
I have not used XHTML, regularly, in years. In fact, when I was writing all the markup for this site myself, I dropped XHTML back in 2004. Instead, I wrote strict compliant HTML 4.01. But nowadays I'm switching to HTML5 for all of my new work. It simplifies several things and adds some awesome new features (which more and more browsers are implementing). But HTML5 seems to also be pulling in some XHTML ideas.
I had a need to link to a specific section of a document earlier. So, naturally, I created an anchor element and gave it a 'name' attribute. The editor I was using gave me a validation warning that HTML5 doesn't support the name attribute on the anchor element. This confused me, so I had to research it. Not able to find any information regarding this issue in the draft specification, I turned to the IRC. In #whatwg on irc.freenode.org I got the answer. Turns out, XHTML 1.0 deprecated the name attribute and the HTML5 working group brought that decision along as making the name attribute "obsolete." Instead, the id attribute is to be used.
Further, the 'a' element, in HTML5, is now strictly considered a "hyperlink" and not either an "anchor" or a "hyperlink." So the real recommendation is to give a parent element, such as a div, an id attribute and create links to that element. In other words, this (HTML 4.01):
<p><a name="foo">bar</a></p>
Becomes this (HTML5):
<p id="foo">bar</p>
Hopefully you won't be as confused as I was, now.
Comments
Ben Simpson said (2010-11-07 15:25:39 GMT):
Good find! I had the same issue a few weeks back and was stumped by the name attribute being deprecated in XHTML. Also, I think we had an issue where IE6 won't link in place unless the anchor element is inside a block element. We had it just inside the body tag. Thats just IE being IE though...
Wayne said (2011-08-15 20:27:26 GMT):
Thanks for this!
melchior blausand said (2011-12-12 01:31:51 GMT):
Thanks a lot! Works like a charm using <a href="#notAnAnchor">oldStyle inDocument Link</a> and elsewhere a <p id="notAnAnchor">
I considered the usage of <a> for both link AND anchor a semantic weakness anyway, and i am happy to see this has been "fixed" in a fashion that makes what was "anchors" much more powerful in HTML5.