Anchor tag inside <h1> or <h1> inside anchor tag: which is better?

78,663

Solution 1

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

Solution 2

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

Solution 3

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.

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 anchor around the header, the whole line get's clickable.

I've made you an example on jsFiddle.net

Solution 4

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.

Solution 5

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>

competing with

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

based on: http://thenewcode.com/617/How-To-Add-Microdata-To-Your-Blog

Share:
78,663

Related videos on Youtube

Sangram
Author by

Sangram

SOreadytohelp Full stack .NET developer, Melbourne, Australia

Updated on September 18, 2022

Comments

  • Sangram
    Sangram almost 2 years

    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?

  • DisgruntledGoat
    DisgruntledGoat over 12 years
    Actually inline elements must not contain block elements as per the HTML spec.
  • Su'
    Su' over 12 years
    @DisgruntledGoat Not quite. Doctype needs to be accounted for.
  • DisgruntledGoat
    DisgruntledGoat over 12 years
    @Su' which doctypes allow block elements inside inline elements?
  • Simon Hayter
    Simon Hayter about 11 years
    either are correct under HTML5
  • Catharsis
    Catharsis almost 11 years
    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>.
  • Martijn
    Martijn about 9 years
    Inline javascript? Are we back in 1999? ;)
  • Martijn
    Martijn about 9 years
    Why would you even do this? Just wrap the header in the anchor. This is just bad practice
  • lucian
    lucian about 9 years
    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.
  • hfarazm
    hfarazm almost 8 years
    I have a question: <h1>hello<h1> // 5 characters <h1><a>hello</a></h1> // 5 characters or google reads it as 12 characters?
  • Arne Kröger
    Arne Kröger about 7 years
    With the hope you will update your answer, it shows, in part, why both examples are NOT equivalent as others have said.