Semantic Headers

September 24, 2005

Recently I stumbled upon an interesting article by Martin Labuschin, in which he describes how to create semantically correct web site headers. He suggests to make use of a h1 element and an arbitrary image replacement technique.

Well at a first glance this approach seems quite obvious and understandable, since the site’s title is a quite important part of the page. But wait. Isn’t there another HTML element designated to holding the site’s title? What about our good old friend the title element? Exactly, this element is definitely the best choice if you want your page’s title to appear on your site — especially because screenreaders tend to read out its content each time you open a HTML page.

Visual Browers

However this approach has one drawback. Visual browsers just display the title in the… well title bar of the browser window. But in most of the cases the title respectively the logo should be displayed prominently within the actual page. So how can we achieve a header displaying the site’s title, both visually appealing and semantically correct? After a short discussion Martin and me came up with a solution that both of us felt confident with. So here we go…

The outcome of this tutorial: a web site header

Markup

We both aggreed on the fact that our header should support users of screenreaders or PDAs in navigating the site. In both cases you have to read the site chronologically — so it is not possible to easily skip between the different sections. Hence we provided a set of links, leading the user directly to the most important areas of the page. In our case these areas are: the main and the secondary content (sidebar).

<div id="header">
  <ul id="quickskip">
    <li><a href="#maincontent">
      skip to primary content</a></li>
    <li><a href="#sidebar">
      skip to secondary content (Sidebar)</a></li>
  </ul>
</div>

Accesskey

Well this is quite practical, but we can further improve accessibility by assigning accesskeys to the respective links. Accesskeys are not just a convenient way for screenreader users to quickly access specific areas of elements on a site, but also for user of visual browsers. If you decide to use accesskeys on your site — and trust me, you should — make sure that you first read this excellent article on accesskey standards on Clagnut. In our case we exemplarily assigned “1” to skip to the main content and “2” for skipping to the secondary content (sidebar).

<div id="header">
  <ul id="quickskip">
    <li><a href="#maincontent" accesskey="1">
      skip to primary content</a></li>
    <li><a href="#sidebar" accesskey="2">
      skip to secondary content (Sidebar)</a></li>
  </ul>
</div>

Another common element that should be accessible via an accesskey is the search field. But in contrast to our “skip” links, the accesskey attribute should be defined directly on the search label.

<label for="search" accesskey="3">Search:</label>
<input type="text" id="search" name="search" />

Navigation

Of course we also want the header to hold our main navigation, so we have to add an additional list of menu items. Now that we have completed our markup, our HTML page looks like this.

<div id="header">
  <ul id="quickskip">
    <li><a href="#maincontent" accesskey="1">
      skip to primary content</a></li>
    <li><a href="#sidebar" accesskey="2">
      skip to secondary content (Sidebar)</a></li>
  </ul>
  <ul id="navigation">
    <li><a href="#">Link 1</a></li>
    <li><a href="#">Link 2</a></li>
    <li><a href="#">Link 3</a></li>
  </ul>
</div>

CSS

Now it is time to turn this plain HTML into a visually appealing header using pure CSS. In a first step we apply some basic styling to our header element: a fixed height as well as a repeating background-image.

#header {
  background: #1e2b4d url(bg_header.gif) repeat-x 0px 100%;
  margin-bottom: 30px;
  height: 105px;
  overflow: hidden;
  position: relative;
}

Hide skip links

Afterwards we have to hide all “skip” links from visual browsers. Note that we don’t use “display: none”, because doing so would also hide the respective markup from screenreaders, which is definitely not intended. Now our page already looks like a header.

#quickskip li {
  position: absolute;
  visibility: hidden;
}

Styling the navigation

In a next step we will style our main navigation. First of all we remove the bullets using list-style: none — and since we don’t want our list items to appear below but next to each other, we float them.

#navigation,
#navigation li {
  float: left;
  list-style: none;
}

Now we positioned the whole navigation absolutely. Important note: don’t forget to assign position:relative to the header — otherwhise the property bottom: 0 would relate to the html element. Preview.

#navigation {
  position: absolute;
  bottom: 5px;
  left: 16px;
}
#navigation li a {
  color: #fff;
  display: block;
  font-weight: bold;
  height: 30px;
  line-height: 30px;
  padding: 0 1em;
  text-decoration: none;
}

Logo

And finally we just have to display the logo.  In order to accomplish this task we assign a background image to the ul element of our skip links. That’s it. Preview

#quickskip {
  background: transparent url(“logo.jpg”) no-repeat 0px 0px;
  height: 70px;
  width: 390px;
}

Finally we can further improve our HTML document by moving the styles to a separate CSS file. Perfect. Here is the final example. If you have any questions or suggestions concerning our approach, don’t hesitate to leave a comment.

14 comments

Toller Artikel! Vielen Dank! Mal schaun, wann ich das umsetze…. P.s: Wolfgang, ich lese deine Seite nun schon seit einem Jahr. Sie ist echt top!

mh

Wenn ihr Probleme mit der Übersetzung habt: Link :)

Martin Labuschin

Good tutorial :)

Jordan

Why is this not on Russian? :)

AKiRA

Perfekt :)

Johannes

Genialer Artikel! Bisher habe ich das h1-Element immer als title-Kopie verwendet, aber das werde ich dann wohl ändern. Ein kleiner Fehler ist aber doch drin: “otherwhise the property bottom: 0 would related to the body element” — html element wäre richtig (oh, und ein d weniger bei related, oder?). Dass SkipLinks und Navigation im div#header zusammengefasst sind, finde ich allerdings nicht so gut. Nur weil sie im Design den Header beinhalten, haben sie semantisch (meiner Meinung nach) nicht wirklich viel mit einem Header zu tun. Hat ein Header überhaupt etwas Semantisches an sich? Eigentlich ist es ja ein reines Design-Element, oder? Ich würde daher das div drumherum weglassen, und der Navigation das Header-Bild als Hintergrundbild zuweisen.

Markus Stange

Numeric accesskeys are a non-starter. The firefox developers added conflicting shortcuts without any warning or discussion between the 1.0 release candidate and the final release. They are now used to switch between tabs.

Jim

You’re probably right —- <code>H1</code>s for site headers suck. After all, when visiting a separate document on a site (an article, for instance), the document itself is most important, not the site’s name. Following that argumentation, you’d wrap the <em>document</em>’s title in the <code>H1</code> instead of the site’s. Yet still, I feel it’s a bit weird to just start a HTML document with navigation links, without actually specifying the site’s name in the <code>BODY</code> first… It’s probably something I’m used to.

Mathias

Hi Wolfgang, concerning PDA compatibility I just wanted to tell you that I tested the example_04 on my MDAII with Internet Explorer for Windows Mobile 2003 and my Palm Treo with Blazer 4.0 as well. On the MDA the example looks quite ok except that the links are aligned vertically, not horizontally. On my Treo the site is not accessible because the links are stacked up in the same place.

Florian Siebert

This is a fantastic topic, but I’m not sure I agree entirely with your conclusion that a document’s title is best left in the <title> tag and nowhere else. (On the other hand, the semantics of my first paragraph would seem to prove your point). It’s my opinion that in order to include a title graphic, banner, or even text header meaningfully in a document, it simply has to have a representative element in the page <body>. This is where content intended for display goes. This is the basic model of web design. To stray from this seems overly academic. Sort of like when people pronouce the word “extra ordinary”. In your example, the title graphic and the page title differ. This might be a sticking point for some as well. Now we’re dealing with an argument based in one person’s terminology, vs. anothers… Perhaps to you, the title of that page is “Sematically correct headers”, but it’s obvious to any user that the title of the page is “my Company”. Using the <h1> tag offers another useful feature: safe failure. If a user agent does not apply CSS properly to the <h1> tag, it’s textual contents will become visible. After all, if the idea of “my Company” is important enough to warrant a large, colorful image at the top of the visual display, it’s surely important enough to be shown to less able user agents. Just my 2 cents (and then some). Just a great topic, though, and I’ll be excited to read some other opinions.

jason carlin

Thank you — interesting input. What about this suggestion: Use h1 elements to markup your site’s title on the homepage — and omit it on all other pages?

Wolfgang

very nice “!”

Tom

Wolfgang, an interesting read. It’s an issue I’ve come accross before, and I’ve never come up with a satisfactory result. Regarding you last comment, instead of using the H1 on the first page and then omitting it in all the others, I suggest using it all on pages, but like this: on your first (home) page, use “SiteName: Home”. On your contact page, use “SiteName: Contact”, and so on.

Bojan

Brillian write up. Thanks. -----

luxuryluke

Commenting is not available in this channel entry.