Webmasters Stack Exchange is a question and answer site for pro webmasters. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I am trying to use <h1> tags on my blog for the post title and came across one problem. Title is hyperlinked.

Case 1:

<h1> <a href=""> xyz </a> </h1>

case 2:

<a href=""> <h1> xyz</h1> </a> 

Which one is better in terms of SEO?

share|improve this question
up vote 42 down vote accepted

If you are using HTML5, just pick one; they're equivalent.
HTML5 does allow block-level links, but in your case there's no particular reason to do it, since there's only one block-level element. Personally, I wouldn't do it here, because having the <h1> tag on the outside would make it easier to scan for in source code.

Anything else (XHTML, HTML4, etc) and the second one is just plain wrong. It wouldn't be valid code, and on some level that's bad for your search optimization [Insert standard disclaimer about how much any single offense really affects anything, etc.].

share|improve this answer
I think i should go for the 1st one. Thanks. – Sangram Oct 3 '11 at 18:42
I have a question: <h1>hello<h1> // 5 characters <h1><a>hello</a></h1> // 5 characters or google reads it as 12 characters? – hfarazm Jul 19 at 13:40

They're the same as far as SEO is concerned. (Usually block level elements contain inline elements and not vice-versa so you should use the first example but it won't affect SEO).

share|improve this answer
Actually inline elements must not contain block elements as per the HTML spec. – DisgruntledGoat Oct 3 '11 at 15:03
@DisgruntledGoat Not quite. Doctype needs to be accounted for. – Su' Oct 3 '11 at 16:07
@Su' which doctypes allow block elements inside inline elements? – DisgruntledGoat Oct 3 '11 at 23:17
@DisgruntledGoat HTML5 allows links (formerly just inline elements) to surround block elements like headers and paragraph tags. This is where doctype is important. If you're using HTML5, by all means use the pattern <a><h1></h1></a>. Otherwise, use the traditional pattern of <h1><a></a></h1>. Google will pay attention to both methods equally, but some browsers may not play along nicely with the non-standard pattern UNLESS you have the correct doctype (HTML5). – Tina Bell Vance Jun 20 '12 at 12:31
so the above will apply for named anchor tags too. isn't it? it is good to have <h1><a name='intro'>Introduction</a></h1> other than <a name='intro'><h1>Introduction</h1></a>. – Jayapal Chandran Aug 23 '13 at 7:34

I find that with Case 2 the href insert is often out of line with the rest of my page. But that could be the way I set my margins in my .css. Thus I would favour Case 1.

share|improve this answer
Thanks Guy Thomas. – Sangram Oct 3 '11 at 18:41

They're both correct in html5, html now allows block elements in inline elements. This also has no effect on SEO, both cases the text is wrapped in the heading, so it remains to have the same value.

Because of this, it's not a choice of validness, but a preference in User Interface.

  • If you wrap the header around the anchor, you create a large anchor, only the text will be clickable.
  • When you wrap the header around the header, the whole line get's clickable.

I've made you an example on jsFiddle.net

share|improve this answer

<h1> <a href=""> xyz </a> </h1> This example is correct.

share|improve this answer
either are correct under HTML5 – Simon Hayter May 3 '13 at 18:16

If the purpose is to have additional clickable elements inside the link (like a picture etc.) and still validate with html<5, you can have it both ways with javascript:

<div onclick="if (this.getElementsByTagName('a')[0]) location=this.getElementsByTagName('a')[0].href;">
<img src="/foo" alt="" />
<a href="#">

else, simply:

<h1 onclick="if (this.getElementsByTagName('a')[0]) location=this.getElementsByTagName('a')[0].href;">
<a href="#">

add cursor:pointer to parent element's css to make the trick complete.

share|improve this answer
Inline javascript? Are we back in 1999? ;) – Martijn Jun 22 '15 at 11:20
Why would you even do this? Just wrap the header in the anchor. This is just bad practice – Martijn Jun 22 '15 at 11:21
Read the comment carefully, you'll find your answer spelled right there ;) Html 4.01 actually is as old as 1999. When you try to validate your suggestion under that doctype you'll get the following error: "[D]ocument type does not allow element "H1" here (...) One possible cause for this message is that you have attempted to put a block-level element (such as "<p>" or "<table>") inside an inline element (such as "<a>", "<span>", or "<font>")." Of course, one doesn't absolutely have to care about what validators say, hence the "if" at the very beginning of the comment. – Lucian Davidescu Jun 22 '15 at 16:04

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.