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>
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.
<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.
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.
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.
<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.
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:
And:
(That assumes that <a>
’s parent can have an <h1>
as its child)
However, it’s not valid under previous versions of HTML:
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With