XML technologies / CSS / Advanced websites with CSS / A simple CSS layout / Building the layout / The header layout
(Excerpt from “CSS − Advanced websites with Cascading Stylesheets” by Rachel Andrew & Dan Shafer)
Now work on the header, containing the logo and main navigation in our design. Recall that we inserted no images when creating the document. We must decide how best to incorporate them. First, insert the logo in an element img. Put the site name in the image’s attribute alt so users disabling images or using screen readers can read it.
Insert the logo immediately after the opening header-div in the document, as shown here:
<body>
<div id="wrapper">
<div id="header">
<img src="img/logo.gif" alt="Footbag Freaks" height="77" width="203" />
<p>The home of the hack</p>
<ul>
<li><a href="">Contact Us</a></li>
<li><a href="">About Us</a></li>
<li><a href="">Privacy Policy</a></li>
<li><a href="">Sitemap</a></li>
</ul>
</div> <!-- header -->View the page in the browser: the image should appear at the off-white box’s upper-left corner.
Our design has fine light-blue lines above and below the title line and navigation. To achieve this, wrap both in another element div and give it top and bottom borders, as shown here:
<body>
<div id="wrapper">
<div id="header">
<img src="img/logo.gif" alt="Footbag Freaks" height="77" width="203" />
<div id="header-bottom">
<p>The home of the hack</p>
<ul>
<li><a href="">Contact Us</a></li>
<li><a href="">About Us</a></li>
<li><a href="">Privacy Policy</a></li>
<li><a href="">Sitemap</a></li>
</ul>
</div> <!-- header-bottom -->
</div> <!-- header -->The rule #header-bottom lets us add those borders:
#header-bottom {
border-top: 1px solid #b9d2e3;
border-bottom: 1px solid #b9d2e3;
}Style the title line and navigation with a few simple text-formatting properties that should be quite familiar now.
#header-bottom ul {
margin: 0;
padding: 0;
}
#header-bottom li {
display: inline;
}
#header-bottom a:link, #header-bottom a:visited {
text-decoration: none;
background-color: #fdf8f2;
color: #050845;
}
#tagline {
font-weight: bold;
background-color: #fdf8f2;
color: #050845;
font-style: italic;
}Give the paragraph containing the title line an attribute id identifying it:
<p id="tagline">The home of the hack</p>Figure: Styling navigation list items with display: inline.
First set the list’s margins and padding, using margin and padding, to 0. Then give the elements li in this area the property display with value inline. All entries now appear on one line rather than each starting a new line, as illustrated above. We also styled navigation links: they use the dark-blue background color, without underlining. The title line is bold italic in the same color.
There is a problem, however: the navigation links are hard to distinguish. A visible separator, such as the pipe symbol (|), is the preferred solution. That is what the following fragment adds:
<ul>
<li><a href="">Contact Us</a> | </li>
<li><a href="">About Us</a> | </li>
<li><a href="">Privacy Policy</a> | </li>
<li><a href="">Sitemap</a></li>
</ul>Also make list items dark blue (#050845), so the pipe symbols outside the links become dark blue too. The next figure shows the refined header.
#header-bottom li {
display: inline;
background-color: #fdf8f2;
color: #050845;
}Figure: Header text elements styled.
The header is taking shape! Next, align the title line and navigation at the same level. We need a property not yet discussed in detail but increasingly important as we proceed: float.
The float property
float is among the most interesting and widely used CSS properties. Its values are left, right or none, although none, the default, is rarely used. With float, an element leaves its normal position in the surrounding block. The values left or right float it to the enclosing box’s left or right from its normal position. Subsequent box contents flow around it. The property float can apply to any block element.
The property float replaces the obsolete attribute align on the tag img, with the same result. Recent W3C HTML recommendations deprecate the attribute align in favor of the property float. This HTML fragment produces the following figure:
<p><img src="logo.gif" alt="Footbag Freaks Logo" width="203" height="77" style="float: left; padding-right: 1em;" />The Footbag Freaks logo appears to the left of this paragraph. Depending on whether or not I use the CSS <code>float</code> property, I may see more than one line of text beside the logo. The CSS <code>float</code> property replaces the deprecated <code>align</code> attribute of the HTML <code>img</code> element and has an identical effect.</p>Figure: Aligning images and text with the CSS property float.
Compared with the attribute align, the property float has a major advantage: float works on elements other than images, whereas align was limited to images, applets and objects.
Does your element lack dimensions?
Specify the width with width. When using float on elements without defined dimensions, include the property width in the declaration. Images have fixed dimensions; text paragraphs, headings and an element div do not.
The header with float
In this section we examine the property float more closely, creating a layout with its main areas positioned using it. Apply our new knowledge to the title line and navigation: float the title paragraph and add these highlighted rules:
#tagline {
font-weight: bold;
background-color: #fdf8f2;
color: #050845;
font-style: italic;
margin: 0;
padding: 0 0 0 20px;
width: 300px;
float: left;
}Set the property margin to 0 to remove default paragraph margins. Add 20 pixels of left padding and a width of 300 pixels, leaving room on the right as in the design. The property float: left places everything left of the remaining content, the navigation list here.
Save the modified stylesheet and view the page. Navigation should now appear beside the title, like text flowing around the image above. We only need to right-align the navigation and give its list padding to separate it from the viewport’s right edge. These rules produce the next figure.
#header-bottom ul {
margin: 0;
padding: 0 30px 0 0;
text-align: right;
}Figure: Title floated and navigation aligned.
Following along in Internet Explorer, you may have noticed problems: the right-aligned navigation and parts of the light-blue borders sometimes disappear mysteriously. Opening another window may make them reappear, or not.
If so, welcome to Internet Explorer’s wonderful world of CSS bugs. This is the “peekaboo bug,” making content disappear and reappear almost randomly.
IE has many CSS bugs, but most seem to surround a mysterious, nonstandard DOM property named hasLayout. The universe of bugs involving hasLayout is vast and hard to understand, but fortunately fixes are not too difficult. Usually one of several seemingly trivial declarations on the troublesome element works. It can be as simple as adding a height to #header-bottom, like this:
#header-bottom {
border-top: 1px solid #b9d2e3;
border-bottom: 1px solid #b9d2e3;
height: 1%;
}This declaration is called the “Holly Hack,” after its inventor Holly Bergevin.
Add it and voilà, problem solved! The height has virtually no effect in other browsers because the contents of the element div exceed it and determine the actual height. Do not worry if this seems unclear. Just know that adding these declarations to troublesome elements or their parents often corrects IE 6’s bizarre behavior.
One final step remains: add the footbag icon shown to the navigation’s right in the design. First insert it beside the navigation list. Its empty attribute alt prevents screen readers from announcing anything; it is purely decorative. The attribute id has the value ball.
<div id="header">
<img src="img/logo.gif" alt="Footbag Freaks" height="77" width="203" />
<div id="header-bottom">
<p id="tagline">The home of the hack</p>
<ul>
<li><a href="">Contact Us</a> | </li>
<li><a href="">About Us</a> | </li>
<li><a href="">Privacy Policy</a> | </li>
<li><a href="">Sitemap</a></li>
</ul>
<img src="img/header-ball.gif" height="24" width="20" alt="" id="ball" />
</div> <!-- header-bottom -->
</div> <!-- header -->Now absolutely position our first CSS element to place the icon correctly. Its offsets from the document’s top and right are known: the logo height sets the top offset, and the right margin of the element div with ID wrapper sets the right offset. These rules place the ball at the navigation’s end:
#ball {
position: absolute;
top: 110px;
right: 55px;
}The header is complete! The next figure shows the result.
Figure: The finished header.
Copyright © 2006 of the German translation, dpunkt.verlag GmbH
You may print the online version for your private use.
Otherwise, this chapter from “CSS − Anspruchsvolle Websites mit Cascading Stylesheets” is subject to the same conditions as the bound edition: the work, including all its parts, is protected by copyright. All rights reserved, including reproduction, translation, microfilming, and storage and processing in electronic systems.
dpunkt.verlag GmbH, Ringstraße 19, 69115 Heidelberg