94

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?

desertnaut
  • 57,590
  • 26
  • 140
  • 166
Frederik Wordenskjold
  • 10,031
  • 6
  • 38
  • 57
  • 3
    [This question on Pro Webmasters](http://webmasters.stackexchange.com/questions/20446/anchor-tag-inside-h1-or-h1-inside-achor-tag-which-is-better) is what you're looking for – John Conde Nov 17 '11 at 21:29
  • Possible duplicate of [Which is more correct:

    ...

    OR

    ...

    ](https://stackoverflow.com/questions/7023512/which-is-more-correct-h1a-a-h1-or-ah1-h1-a)
    – mhrabiee Jul 17 '19 at 16:12
  • 2
    I’m voting to close this question because, while this was on-topic when asked, non-programming SEO questions are no longer on-topic here and should be asked at [webmasters.se]. Specifically the previously-linked [Anchor tag inside

    or

    inside anchor tag: which is better?](https://webmasters.stackexchange.com/q/20446/100199) answers this question there.

    – dbc Jul 16 '20 at 15:57

3 Answers3

88

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

animuson
  • 53,861
  • 28
  • 137
  • 147
8

If you're using an HTML 4.01 DTD (Not sure about the transitional, but definitely the strict), then the anchor element must appear inside the header element. Check your doctype if you don't know which DTD you're using (assuming you have one, which you do need for oyur code to be considered valid). The HTML5 doctype looks like this:

<!DOCTYPE HTML>

The 4.01 doctype is:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"> 

I'd also suggest familiarizing yourself with the DTD itself if you're going to work with 4.01, it will answer all of these types of questions.

MGZero
  • 5,812
  • 5
  • 29
  • 46
-4

First one is best for the SEO because google index that very easily.