-1

I realise that both wrapping a heading with a link, and putting a link inside the heading both work, but is there a convention or actual reason for doing one over the other.

So for example, is <a href="#"><h1>A Header</h1></a> acceptable, or should I always code <h1><a href="#">Another Header</a></h1>?

A question that everyone has thought of at some point, but was too afraid to ask!

worldofjr
  • 3,868
  • 8
  • 37
  • 49
  • 2
    Actually not everyone has been too scared to ask. See http://stackoverflow.com/questions/5341451/what-should-come-first-in-html-an-anchor-or-a-header – carloabelli Aug 25 '14 at 02:34
  • OK, perhaps I should have said they have been afraid to ask since 2011! – worldofjr Aug 25 '14 at 02:49

1 Answers1

1

As of HTML 5, this is valid. Previously only inline elements could be contained within an anchor tag. The answer in this question gives some details

What elements can be contained within a <a> tag?

Link to the spec http://www.w3.org/TR/html401/struct/global.html

So yes it is valid HTML, whether or not you should do it is up to you. Personally, I would have the link within the H1 tag, as it makes more sense to me to have a block element containing an inline element.

Community
  • 1
  • 1
James Hay
  • 7,115
  • 6
  • 33
  • 57