The <h> Hierarchy: Using Heading Tags the Right Way

September 17th, 2008 in Design Tips & Tutorials

by: Matthew Griffin

If you've been using CSS and web standards for awhile, then you probably already know that the HTML heading tags h1-h6 are meant to denote a hierarchy of importance—the most important, of course, being <h1> and the least important being <h6>. If you're new to CSS layouts and web standards then you might think heading tags just make text bigger or smaller. But there's a lot more to heading tags than most web designers realize. And in order to build beautifully semantic websites that degrade gracefully, a proper understanding of the <h> tags is a must.

Getting the Hierarchy Right

As I mentioned above, heading tags place more or less importance on a block of content depending on the number of the heading. The W3C says:

A heading element briefly describes the topic of the section it introduces. Heading information may be used by user agents, for example, to construct a table of contents for a document automatically.

It's important to progress sequentially through the hierarchy of headings but it's not necessary to have all six on every page. For example, one page might have a main title(<h1>) and then a subtitle (<h2>) and that's it. This is perfectly acceptable. However, if the page were to jump from <h1> straight to <h3>, that would technically be incorrect. Although the page would still render perfectly fine in a browser, converting the document to other presentations could cause problems. It's best to just do it right the first time.

Also, you should only have one <h1> tag per page. The <h1> tag is meant for the most important heading on the page. Having more than one most important heading wouldn't make any sense. However, once the most important heading (usually the page title) has been established, you may use as many h2-h6 tags as you find appropriate.

Using <h> Tags in the Layout

When we think about heading tags, the first thing that comes to mind (especially for those of us who cut our teeth on Front Page) is body copy. The <h1> tag would be at the top of the body copy and the rest of the <h> tags would follow in the text below.  But a  well-structured web page uses <h> tags where they are semantically appropriate—where they most effectively describe the content they contain. For this reason, many times, layout elements should be <h> tags rather than generic <div> tags. Remember, <h> tags are block-level elements by default just like <div> tags, which means they can do everything a <div> tag can do.

A lot of designers (myself included) have been guilty of thinking that CSS layout is the same as semantic layout. This just isn't the case. Let's take a look at some sample code that uses well-structured XHTML but is semantically meaningless:

<div id="header">
    <div class="logo"><img src="logo.gif" alt="Mirificam Press" /></div>
    <div class="tagline"><img src="tagline.gif" alt="Information for Web Designers" /></div>
</div>

Structurally, there is nothing wrong with this markup. Add a little CSS and it will look great in just about any browser. The problem is that the generic <div> has no meaning—it doesn't describe the content it holds. So, for example, a mobile device with limited CSS support,  or a screen reader would have no idea that your logo is the most important title on your page. The page doesn't degrade gracefully at all—it degrades to a big pile of mushy text. This is how it should have been marked up:

<div id="header">
    <h1>Mirificam Press</h1>
    <h2>Information for Web Designers</h2>
</div>

Not only is this markup semantically correct, it's more concise and readable than our original example. Now, to turn our <h1> and <h2> tags into images for CSS supporting browsers, we just add a little CSS that might look something like this:

#header h1 {
    background: url(logo.gif) no-repeat;
    text-indent: -9999px; /* This gets the text out of the way */
}
#header h2 {
    background: url(tagline.gif) no-repeat;
    text-indent: -9999px; /* This gets the text out of the way */
}

You would also have to add height and width attributes to the <h1> and <h2>  to correspond with the dimensions of the logo.gif and tagline.gif images, but other than that, you're ready to go. The content is completely separated from the style/presentation which is exactly how it should be. You probably also noticed that the text-indent is set to -9999px on the <h1> and <h2> tag. This is a trick I learned from Designing with Web Standards by Jeffrey Zeldman. It pushes the text out of the visible area in browsers that support CSS. Everything that doesn't support CSS sees the text.

  • 15 Comments
  • 7996 Views

Comments

Posted By: Chad on 09/17/08

Good article. I've always been one to use the H1 tag for the site title, and H2 for page titles. Occasionally I'll come across someone screaming about how you should use the site title as an H2, and your page title as H1, as your page title is what that page is about. What do you think about that? And is it semantically incorrect to do it that way?

Posted By: Winnie Lim on 09/17/08

Is there a simple solution if we need the logo to be clickable?

Posted By: Brandon Cox | eGrace Creative on 09/17/08

Excellent - this is the kind of article that really contributes to a better web! As far as the logo of the page, whether it makes a difference or not is debatable, but I usually use a p tag inside a div with an id of either site-title or logo. Again, search engines may or may not use this information, but some suspect Google at least is getting smarter at looking for attributes such as these. My own confession? I sometimes use an h6 or h7 for a special, out of the ordinary element such as an announcement in the sidebar. It's not the place for a subheading, but I don't want to overuse pseudo-classes either. I'll try to cut back. Thanks!

Posted By: Matthew Grffin on 09/17/08

I agree, Brandon. I can see it both ways. Personally, I think the H1 with the site title in it just works better, but I'm not rigid about that rule.

Posted By: Matthew Grffin on 09/17/08

Winnie Lim, if you want the logo to be clickable, just wrap your logo title in a link tag and set that tag to block level display. You can see more about this technique in my article titled "Rollover Lite".

Posted By: Ville Väisänen on 09/18/08

Wow, neat trick Matthew. Thanks for sharing.

Posted By: Du on 09/21/08

Good article, I've always wondered about the proper usage of header tags and thanks for clarifying. Just wondering though, is it correct to use <h1> tags for both the site title and page title?

Posted By: Matthew Griffin on 09/22/08

No, you'll want to choose one or the other. There's some pretty good discussion of this above so you can see some of the pros and cons of both.

Posted By: Todd on 05/27/09

I was wondering if the H Tags need to have corresponding sizes attached to them. I.e. Should a H1 tag be 20 point, a H2 tag 18 points, H3 16 point etc. Will the Search Engines penalise you for example if the H2 tag is larger in size than the H1 tag?

Posted By: Matthew Griffin on 05/27/09

Todd, as far as I know there is no penalization. As far as style goes, feel free to make your headings whatever size you want.

Posted By: Todd on 06/02/09

Thanks Matthew. One further question if it's ok. Do Search Engines compare tags across sites? For example, if on my site I use only H2 tags on my Product Pages. I.e. H2 for the title of the product and H2 for subheads relating to the product, but on someone else's site they use H1 for the product title and H2 for some subheads all other things being equal, would I rank lower than a site that has used the H1 against that particular product search term?

Posted By: Matthew Griffin on 06/02/09

Todd, it's hard to say. There's no official statement from Google saying so but we don't really know how the algorithm works. Even among the elite SEOistas there is some discrepancy on the issue. One thing you might consider is having product title as H2s on search results but have them as H1s on the detail page for each product. That way you're covering both. The main thing is that the content is well formatted and that you have quality links pointing your way.

Posted By: Todd on 06/02/09

Thanks Matthew - appreciate the quick reply and extra help. All the best. T

Posted By: Stefanos on 11/24/09

In my opinion the H1 tag should should always: �Be used only once in each page[1] �Contain target keywords �Be descriptive and unique �Be placed at the top of each page �Be placed under the breadcrumb trail. �Styled using an external CSS rule

Post Your Comment

Comments are closed.