Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What should come first in HTML, an anchor or a header? [duplicate]

Tags:

html

semantics

I'm wondering which of the following two orders is semantically correct in HTML:

1. <h1><a>Header</a></h1>
2. <a><h1>Header</h1></a>
like image 789
Mohamad Avatar asked Mar 17 '11 15:03

Mohamad


People also ask

How do you anchor a header in HTML?

Add a custom anchor To add an anchor to a heading in HTML, add a <section> element with an id attribute. Don't use <a name> . Use lowercase for id values, and put hyphens between words. To add an anchor to a heading in Markdown, add the following code to the end of the line that the heading is on.

What is the correct tag of an HTML anchor?

<a>: The Anchor element. The <a> HTML element (or anchor element), with its href attribute, creates a hyperlink to web pages, files, email addresses, locations in the same page, or anything else a URL can address.

What goes between opening and closing anchor tags?

Anchors. An anchor is a piece of text which marks the beginning and/or the end of a hypertext link. The text between the opening tag and the closing tag is either the start or destination (or both) of a link.

What is an anchor tag in HTML and why do you need to use it?

The <a> tag (anchor tag) in HTML is used to create a hyperlink on the webpage. This hyperlink is used to link the webpage to other web pages or some section of the same web page. It's either used to provide an absolute reference or a relative reference as its “href” value.


2 Answers

<h1><a>Header</a></h1>

<h1> is a block-level element and <a> is not, it is syntactically invalid HTML to have block level elements inside inline elements (at least until HTML5) which is how the other way would be.

like image 124
Paolo Bergantino Avatar answered Sep 20 '22 02:09

Paolo Bergantino


This answer on a duplicate question is better than mine: https://stackoverflow.com/a/7023551/20578

But, for posterity:


Semantically, there’s no difference. Remember, “semantic” just means “related to meaning”, and meaning is just something agreed between humans (because computers don’t natively do meaning, that’s a human brain thing). No-one’s got time to agree that one of these virtually identical options means something different to the other :)

Surprisingly, they’re actually both valid as well, as of the current HTML spec, because <a>’s content model is defined as “transparent”, i.e. the same as its parent.

See:

  • http://www.pauldwaite.co.uk/test-pages/5341451

And:

  • http://html5.validator.nu/?doc=http%3A%2F%2Fwww.pauldwaite.co.uk%2Ftest-pages%2F5341451%2F&showsource=yes

(That assumes that <a>’s parent can have an <h1> as its child)

However, it’s not valid under previous versions of HTML:

  • http://validator.w3.org/check?uri=http%3A%2F%2Fwww.pauldwaite.co.uk%2Ftest-pages%2F5341451&charset=%28detect+automatically%29&doctype=HTML+4.01+Transitional&group=0
like image 22
Paul D. Waite Avatar answered Sep 21 '22 02:09

Paul D. Waite