XML technologies / CSS / CSS Cookbook / Links and navigation / Dynamic visual menus

(Excerpt from “CSS Kochbuch” by Christopher Schmitt)

Problem

Create a tabbed navigation menu with rounded corners that still displays correctly when text size changes. The following figure shows its default appearance.

Dynamic tabbed navigation menu

Figure: The dynamic tabbed navigation menu.

Solution

Start by creating the navigation menu’s markup:

<div id="header">
  <h2>Persönliche Site dot-com</h2>
  <h5>Navigationsmenü:</h5>
  <ul>
    <li><a href="/">Startseite</a></li>
    <li><a href="/about/">Über uns</a></li>
    <li><a href="/archives/">Archive</a></li>
    <li><a href="/writing/">Schreiben</a></li>
    <li id="current"><a href="/speaking/">Sprechen</a></li>
    <li><a href="/contact/">Kontakt</a></li>
  </ul>
</div>

Then create two tab images: one for links and one for the currently selected page. Split each image into two separate files, as in the next figure.

Two-part image for one of the tab graphics

Figure: A two-part image for one of the tab graphics. Note the rounded upper corners.

Place the tab’s right side in the list item’s background:

#header li {
 float: left;
 background-image: url(tab_right.gif);
 background-repeat: no-repeat;
 background-position: right top;
 margin: 0;
 padding: 0;
}

Define the tab’s left side as the anchor element’s background:

#header a {
 display: block;
 background-image: url(tab_left.gif);
 background-repeat: no-repeat;
 background-position: left top;
 padding: 5px 15px;
 color: #ccc;
 text-decoration: none;
 font-family: Georgia, Times, "Times New Roman", serif;
}

To show the currently viewed document in the navigation bar, assign the corresponding tab image:

#header #current {
 background-image: url(tab_right_current.gif);
}
#header #current a {
 background-image: url(tab_left_current.gif);
 color: black;
}

Place the image with the one-pixel-high line below the group of tabs.

Discussion

Using text for navigation links serves three web development criteria:

  • Accessibility

  • Design

  • Maintainability

For example, visually impaired users can adjust the navigation menu as needed, as in the following figure.

The enlarged navigation menu

Figure: The enlarged navigation menu.

Because users can enlarge text considerably, the tab background images must also be quite large or the design breaks (see the next figure). Each tab graphic in this solution is therefore 450 pixels wide.

The design of some tabs breaks

Figure: The design of some tabs breaks.

Many web developers prefer this method because the link list is easy to maintain. To change a label or correct a typo, simply edit the HTML without opening an image editor.

Another advantage is that the tabs can be more visually appealing. The recipe Horizontal navigation menus explains how to create tabbed navigation with the property border, but without rounded corners. This recipe, by contrast, lets developers add rounded corners or gradients for a more attractive appearance.

See also

The recipe Pull quotes with images, which describes the so-called rubber-band method for creating pull quotes with images, and the article “Sliding Doors of CSS, Part II”, which also discusses this kind of tabbed navigation.

  

 

 

 

back to top