I am trying to use
<h1> tags on my blog for the post title and came across one problem. Title is hyperlinked.
<h1> <a href=""> xyz </a> </h1>
<a href=""> <h1> xyz</h1> </a>
Which one is better in terms of SEO?
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.].
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).
They're both correct in html5, html 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.
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.
What's said here's is insightfull, thank you all. Let's take it up one more notch: adding microdata and such into the equation.
Let's say we've got
<h1 itemprop="name"><a href="http://goldenage.com/maths.html" itemprop="url">Mathematics in The Muslim Golden Age</a></h1>
<a href="http://goldenage.com/maths.html" itemprop="url"><h1 itemprop="name">Mathematics in The Muslim Golden Age</h1></a>
To me, 'regardless of the performance', example 2 makes more sense. Because the link is never part of the name. The difference boils down to the difference between innerHTML and textContent, DOMwise. Looking at it through innerHTML, the anchor gets in the way. If textContent were the way, tags would be stripped. So that also puts the question: innerHTML or textContent.
So I would say, taking microdata into account, having the anchor on the outside is more pure.
<h1> <a href=""> xyz </a> </h1> This example is correct.
Block level links should be avoided for SEO purposes - from the horse's mouth: https://www.seroundtable.com/block-level-links-google-seo-16369.html
Update: Takeaways from the link...
Having either linking construct, as others have said, is fine for linking. However, for SEO purposes, you should keep the anchor text clean so Google can interpret the anchor better and assign the appropriate relevance.
John Mueller (Webmaster Trends Analyst at Google) goes on to say...
That usage would be fine with us (Google) - we'd still pick up the link, and would be able to associate your text as an anchor with that. We're pretty flexible with parsing HTML, so you could probably even use this with HTML4. That said, the clearer you make your anchor text, the easier it is for us to understand the context of the link, so I wouldn't necessarily always use a whole paragraph as the anchor for all of your internal links.
TL;DR For SEO, don't use block-level link.
<div onclick="if (this.getElementsByTagName('a')) location=this.getElementsByTagName('a').href;"> <img src="/foo" alt="" /> <h1> <a href="#"> linked-heading </a> </h1> </div>
<h1 onclick="if (this.getElementsByTagName('a')) location=this.getElementsByTagName('a').href;"> <a href="#"> linked-heading </a> </h1>
cursor:pointer to parent element's css to make the trick complete.