Anchors inside headers, or vice versa - is there a different in relation to SEO?

Frederik Wordenskjold picture Frederik Wordenskjold · Nov 17, 2011 · Viewed 22.2k times · Source

I always stumble upon this (rather simple question) when making e.g. Wordpress blogs. Should I wrap the headline, which should also be a link, in a anchor <a> element, or should it be the other way around. And why?

(1):

<a href="foo">
    <h1>bar</h1>
</a>

(2):

<h1>
    <a href="foo">bar</a>
</h1>

I always end up doing (1), as it just seems more logical to me, that the headline is wrapped in a link.

Does it make any difference in e.g. SEO? Will it reflect the SEO ranking?

Answer

animuson picture animuson · Nov 17, 2011

Prior to HTML5:
The anchor has to be inside the header, you cannot put a block-level element inside an anchor, and most browsers will not render it 100% reliably if you do.

In HTML5:
It doesn't matter, use whichever one makes the most semantic sense. Likely the first one.

Remember that if your document is using HTML4 DTD's, it will not validate and may not render correctly because it's using the old rules where an anchor cannot contain a block-level element. Only use the first option in HTML5. XHTML is equivalent to HTML4, I'm not 100% sure about XHTML1.1 though (try it and see if it validates).