Definition and Usage The <h1> to <h6> tags are used to define HTML headings. <h1> defines the most important heading. <h6> defines the least important heading. Note: Only use one <h1> per page - this should represent the main heading/subject for the whole page.
The H1 should almost always be the largest text on the page. So an H1 is always BIG, H2 slightly less so. This is the default styling. The H1 tag is given prominence, it is the title or main heading.
The H1 tag is an HTML heading that's most commonly used to mark up a web page title. Most websites use CSS to make the H1 stand out on the page compared to lesser headings like H2, H3, etc.
Both versions are correct. The biggest difference between them is that in the case of <h1><a>..</a></h1>
only the text in the title will be clickable.
If you put the <a>
around the <h1>
and the css display
property is block
(which it is by default) the entire block (the height of the <h1>
and 100% of the width of the container the <h1>
resides in) will be clickable.
Historically you could not put a block element inside of an inline element, but this is no longer the case with HTML5. I would think that the <h1><a>..</a></h1>
approach is more conventional though.
In the case where you want to put an anchor on the header, a better approach than <a id="my-anchor"><h1>..</h1></a>
would be to use either the id
or the name
attribute like this: <h1 id="my-anchor">..</h1>
or <h1 name="my-anchor">..</h1>
In pre HTML 5 this one
<a><h1>..</h1></a>
will not validate. You can use it in HTML 5. However, i would use this:
<h1><a>..</a></h1>
unless you need to add more than < h1 > inside the < a >
<a><h1></h1></a>
is not W3C valid... Basically, you can't put block elements inside inline elements
<h1><a>..</a></h1>
and <a><h1>..</h1></a>
have always behaved almost the same, when style sheets do not affect the rendering. Almost, but not quite. If you navigate using the tab key or otherwise focus on a link, a “focus rectangle” appears around the link in most browsers. For <h1><a>..</a></h1>
, this rectangle is around the link text only. For <a><h1>..</h1></a>
, the rectangle extends across the available horizontal space, since the markup makes the a
element a block element in rendering, occupying 100% width by default.
The following shows how a focused <a href=foo><h1>link</h1></a>
is rendered by Chrome:
This implies that if you style elements e.g. by setting a background color for links, the effects differ in a similar manner.
Historically, <a><h1>..</h1></a>
was declared invalid in HTML 2.0, and subsequent HTML specifications followed suit, but HTML5 changes this and declares it as valid. The formal definition has not affected browsers, only validators. However, it is remotely possible that some user agents (probably not normal browsers, but e.g. specialized HTML renderers, data extractors, converters, etc.) fail to handle <a><h1>..</h1></a>
properly, since it has not been allowed in the specifications.
There is seldom a good reason to make a heading or text in a heading a link. (It’s mostly illogical and bad for usability.) But a similar question has often arised when making a heading (or text in a heading) a potential destination for a link, using e.g. <h2><a name=foo>...</a></h2>
versus <a name=foo><h2>...</h2></a>
. Similar considerations apply to this (both work, there may be a difference since the latter makes the a
element a block, and before HTML5, only the former is formally allowed). But in addition, both ways are outdated, and using the id
attribute directly on the heading element is now recommended: <h2 id=foo>...</h2>
.
H1 elements are block level elements, and anchors are inline elements. You are allowed to have an inline element within a block level element but not the other way around. When you consider the box model and the HTML spec this makes sense.
So in conclusion the best way is:
<h1><a href="#">Link</a></h1>
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