Publishers of technology books, eBooks, and videos for creative people

Home > Articles > Web Design & Development > CSS

  • Print
  • + Share This
This chapter is from the book

Styling the Document

Basically, we have two main tasks ahead of us:

  • To make the page look like it did when it relied on HTML-based presentation

  • To push the icons to a new level of visual effect by applying some creative CSS to them

Getting Back to Square One

Before we get down 'n' dirty with the links, let's quickly reproduce the original basic design look in CSS. Because we have the HTML file to guide us, we can just rewrite the styles to match what we had before (see Figure 4.3).

<style type="text/css">
body {background: #CEC; color: black;}
td#navbuttons {background: #ACA; padding: 0;
  border: 2px solid #797;}
td#main {background: #FFD; color: black;
  border: 2px solid #797;}
</style>

Figure 4.3Figure 4.3 The first step in re-creating the basic design.a

The space between the two cells is now 4 pixels thick, thanks to the fact that there are two adjacent borders and each is 2px thick. We need to reduce one of them to zero or both to be 1 pixel wide. Let's try the latter:

td#navbuttons {background: #ACA; padding: 0;
  border: 2px solid #797; border-width: 2px 1px 2px 2px;}
td#main {background: #FFD; color: black;
  border: 2px solid #797; border-width: 2px 2px 2px 1px;}

Alternate Border Effects

We also could have created the borders around the cells by setting a background color for the table itself and then setting a value (such as 2) for the cellspacing attribute. Although this approach works in some cases, it also tends to rob the designer of flexibility because it enforces a single padding on all cells instead of allowing different amounts of padding on different cells. That's why we're avoiding it here.

We should also set the vertical and horizontal alignment of the content within the cells. We know that both the icons and the text should be aligned to the top of their table cells, and the icons ought to be center aligned within their cells (see Figure 4.4). Thus:

body {background: #CEC; color: black;}
table#inform td {vertical-align: top;}
td#navbuttons {background: #ACA; padding: 0;
  border: 2px solid #797; border-width: 2px 1px 2px 2px;
  text-align: center;}

Figure 4.4Figure 4.4 Everything's back (more or less) to where we started.

The only thing left to do would be to reproduce the effect of the attribute cellpadding="5" in the original file. We could do that with padding, but we're going to put it off until later when we have a better idea of how the layout might be affected by padding on the cells.

Upgrading the Title

Before we get to the links, we need to make the title fit in with the rest of the design. The design department, you might recall, suggested that we eliminate the space between the text and the table (see Figure 4.5). They probably meant that we should set the bottom margin to zero, but let's take them literally at their word:

body {background: #CEC; color: black;}
h1 {margin-bottom: -0.25em;}
table#inform td {vertical-align: top;}

Figure 4.5Figure 4.5 Get rid of the space between text and table? You got it!

It still doesn't fit in too well, so let's change the color to match the medium-green borders and also switch it to be a sans-serif font. While we're in the area, we'll also boldface it and make sure it's twice the normal text size.

body {background: #CEC; color: black;}
h1 {margin-bottom: -0.25em;
  font: bold 200% Arial, sans-serif; color: #797;}
table#inform td {vertical-align: top;}

There's one more thing that would make this work even better, and that's a thicker top border on the table. Let's make it easy and just add the border to the table itself instead of messing with the table cells (see Figure 4.6).

h1 {margin-bottom: -0.25em;
  font: bold 200% Arial, sans-serif; color: #797;}
table#inform td {vertical-align: top; border-top: 3px solid #797;}

Now it looks like the title is rising from the border itself or maybe was carved out of the same stuff. Whatever visual metaphor it invokes, it's an interesting effect. We'll keep it and see what the client thinks.

Figure 4.6Figure 4.6 Making the title part of the organic whole.

The Icons

The relatively simple nature of the icons (each is a single image alone in a link element) makes them easier to work with. We'll tackle the left-side icons first. We know that each icon is 50x50 pixels. We also know that we want them to sit in the left-side panel with no extra space around them, so we need to convert them to block-level elements with no margin. But we need to be careful about what we convert!

td#main {background: #FFD; color: black;
  border: 2px solid #797; border-width: 2px 2px 2px 1px;}
td#navbuttons a {display: block; margin: 0;}
td#navbuttons img {display: block; height: 50px; width: 50px;}
</style>

This won't have any immediate visual impact, but it avoids trouble in the next step. We want to increase the amount of space around each image, but rather than doing it with margins, let's do it with borders that exactly match the background color of the cell. We'll also set the background color of the images to be transparent so that the cell background remains visible around each icon.

td#navbuttons a {display: block; margin: 0;}
td#navbuttons img {display: block; height: 50px; width: 50px;
  border: 1px solid #ACA; border-width: 5px 10px;
  background: transparent;}
</style>

Okay, so besides adding some apparently empty space around the icons, what good did this do? Plenty. Assume that the current page is the Natural Gas page. We can highlight the icon by adding a rule that makes the border and background the same color as the intracell borders (see Figure 4.7).

td#navbuttons a {display: block; margin: 0;}
td#navbuttons img {display: block; height: 50px; width: 50px;
  border: 1px solid #ACA; border-width: 5px 10px;
  background: transparent;}
td#navbuttons img#gas {border-color: #797; background: #797;}
</style>

The big win we get here is not just that we can easily indicate the current page, but also make the background and border colors change when the link is hovered over by the mouse pointer or when the icon is clicked.

Figure 4.7Figure 4.7 Highlighting an icon with borders and background

Visitation Styles

We'll skip writing a "visited" style for the icons, although we could create one easily enough. As an example, we could have written td#navbutton a:visited {border-color: gray;}..

td#navbuttons img#gas {border-color: #797; background: #797;}
td#navbuttons a:hover {background: yellow;}
td#navbuttons a:hover img { border-color: yellow;}
td#navbuttons a:active img {border-color: #FC0;
  border-style: inset;}
</style>

Two Blocks?

Yes, we've set both the hyperlinks and the images to be block-level elements. By making both elements blocks, we can be assured that they'll behave in a predictable way—sort of like one div inside another. If we left the images alone, they would default to being inline elements, which can cause unexpected space to appear in recent browsers.

Now any link (other than the one for the current page) will get a yellow background when hovered over. If an icon is clicked, its border will turn orange, thus framing the link for a moment in a thick orange box with the yellow background still visible inside (see Figure 4.8).

Figure 4.8Figure 4.8 Combining hover and active styles can lead to interesting effects.

It's worth spending a moment on the selectors. Take, for example, td#navbuttons a:hover img. It's written this way because we want to give a yellow highlight to any image that's descended from a link being hovered over—both of which are contained within a td element with an id of navbuttons. Ditto for the "active" rule.

It's worth asking, though, why we set the background color on the hyperlink instead of for the image itself. It turns out that IE5.x for Windows mostly ignores background styles on images that are part of hovered links. This failure is very odd because it will change the border color, but there you have it. Because IE5.x will set the background color of the hyperlink, we can sneak around this bug in the manner shown. If you're developing for a situation in which IE5.x isn't an issue, you could just style the background of the image and not mess with the link's background at all.

No Hover for the "Current" Icon

So why doesn't the icon for the current page (the gas flame) take on the hover or active styles? Because the specificity of its selector (td#navbuttons img#gas) outweighs the selectors for the hover and active states, so its values for the border and background colors win out.

Altering the Main-Text Links

With the left-side icons working the way we'd like, let's give the text links a makeover. Our first order of business is to define a "baseline" for the text links. Typically, designers will change the color of a link in its various states, and sometimes they'll forcibly remove the underlines.

In this case, we're just going to change the colors but leave the underlines alone. That way, the user's preference setting regarding link underlining will hold sway, which will help them recognize links for what they are. Because the blue doesn't really work with our green-and-sand color scheme, though, we're going to make the links a dark green when unvisited and dusky purple when visited. Just to make sure the links stand out, let's boldface them as well.

td#navbuttons a:active img {border-color: #FC3;
  border-style: inset;}
a:link, a:visited {background-color: transparent; font-weight: bold;}
a:link {color: #171;}
a:visited {color: #747;}
</style>

Now we need a good hover style. Actually, we need two good hover styles: one for unvisited links and one for visited links:

a:visited {color: #747;}
a:visited:hover {color: #FFD; background-color: #747;}
a:link:hover {color: #FFD; background-color: #797;}
</style>

Splitting Up the Styles

We split the styles between the a:link, a:visited rule and the a:link and a:visited rules to keep them as simple as possible. Otherwise, we would have been duplicating the background-color and font-weight styles for both link states, which doesn't make much sense.

Now we get a reverse-text effect on all our links. In CSS2-aware browsers, we'll get yellow-on-green for hovered unvisited links and yellow-on-purple for hovered visited links (see Figure 4.9). It doesn't matter what order these rules come in because they can never conflict with each other. That's because a link can't be both visited and unvisited.

Figure 4.9Figure 4.9 When changing the appearance of links, it's best to make sure they still stand out.

Dealing with Explorer

If you use the form of "chained" selector shown for your hover styles, make sure you put the default second—that is, whichever hover style you'd prefer to be applied to all links in a document, visited or otherwise. Explorer doesn't understand this syntax, so it will treat all such rules as if they're simple a:hover rules.

Another problem you might encounter in Explorer is that it thinks the last link that was clicked is still active. Therefore, if you click a link and then hit the Back button, the page will come up with the link still in the active state even though it isn't active. Given this fact, you might want to avoid writing a:active styles if Explorer users will make up a big portion of your audience.

Help! A Press Release!

Now that we've done the basic style work on text links, let's jazz up the help and press-release links. The icons are cute enough, but we can do something a lot more interesting than having these graphics embedded in the page itself.

The first thing we need to do is remove the icons from the HTML and create taller versions—say, 32 pixels high instead of 16. The important thing is that the icons should be an even number of pixels tall.

NOTE

"Taller" images already exist in the files you downloaded from the Web site: help-icon.gif and pr-icon.gif.

Now let's put a border around the help link, place the icon in the background, position it on the left side and centered vertically, set padding to keep the text from overlapping the icons, and also change the text and background colors to go along with it (see Figure 4.10). Oh, and just for the heck of it, we'll eliminate the underline, too.

Figure 4.10Figure 4.10 Taking a text link from "blah" to "boo-yah!"

a:link:hover {color: #FFD; background-color: #797;}
a.help:link, a.help:visited {padding: 0 2px 1px 16px;
  background: #FDD url(help-icon.gif) left center no-repeat;
  color: #733; border: 1px solid #C66;
  text-decoration: none;}
</style>

By aligning the background image with the left centerpoint of the link (using the keywords left center), we can make it look like it's inline. As for the padding, it helps keep the borders pushed a little bit away from the text and opens up enough space on the left to show the background image. It's easy enough to adjust the padding as necessary (for example, to close up the space between the edge of the icon and the text).

Removing the Images

Remember to remove the img elements from the help and press-release links in the HTML document itself. If they're left in, they will obscure the background images we're inserting and greatly interfere with the intended effect.

It looks like the icon is still part of the document, and that's exactly what we want. The advantage of putting it in the background of the link, of course, is that we can easily change it later without having to touch the document source. We might decide to put the icon on the right side of the hyperlink, for example. Doing that would be a simple matter of changing the values for padding and background—nothing more.

Let's give the same treatment to the press-release link, using its icon and colors to match:

a.help:link, a.help:visited {padding: 0 2px 1px 16px;
  background: #FDD url(help-icon.gif) left center no-repeat;
  color: #733; border: 1px solid #C66;
  text-decoration: none;}
a.pr:link, a.pr:visited {padding: 0 2px 1px 16px;
  background: #EEC url(pr-icon.gif) left center no-repeat; 
  color: #171; border: 1px solid #797;
  text-decoration: none;}
</style>

The only real differences are in the colors and the image; otherwise, everything's the same. Now all we need are some good hover effects for the links, and we'll be golden (see Figure 4.11):

a.pr:link, a.pr:visited {padding: 0 2px 1px 16px;
  background: #EEC url(pr-icon.gif) left center no-repeat; 
  color: #171; border: 1px solid #797;
  text-decoration: none;}
a.help:hover {color: #FFD; background-color: #C66;}
a.pr:hover {color: #FFD; background-color: #797;}
</style>

Figure 4.11Figure 4.11 Now there are two way-cool links for our viewing pleasure.

Changes on Hover

In theory, you could also change the background image in the hover state, but Explorer 5.x for Windows doesn't handle hover-based changes very gracefully. Its usual behavior is to change the background image when you hover over a link and then keep the hover image after the mouse moves off the link. Sadly, there doesn't seem to be a CSS-based way around this bug.

Now let's create some "visited" styles for our way-cool links. We could do the usual and change the various colors, but let's take it a step further and display a different background image—thus, changing the icon for visited links.

The basic need here is for new images. We'll go with ones that look "washed out" because they're the easiest to produce. Then all we need to do is create the styles to drop them into place when a link's been visited, as well as some color shifts.

a.pr:link, a.pr:visited {padding: 0 2px 1px 16px;
  background: #EEC url(pr-icon.gif) left center no-repeat; 
  color: #171; border: 1px solid #797;
  text-decoration: none;}
a.help:visited {color: #A88; background-color: #EDD; 
  background-image: url(help-vicon.gif);}
a.pr:visited {color: #797; background-color: #DDC; 
  background-image: url(pr-vicon.gif);}
a.help:hover {color: #FFD; background-color: #C66;}

NOTE

Look for the files help-vicon.gif and pr-vicon.gif in the files you downloaded from the Web site. These are the washed-out versions of the link icons we've been using (see Figure 4.12).

Figure 4.12Figure 4.12 Visitation changes: Washing out a link after it's been visited helps users remember where they've been.

Of course, we could have used any icon at all—one with a little "X" over the icon, maybe an inverse image in which the colors are all reversed, or really anything. The only limitation is what you can fit into the space.

Order in the Link States

We've added the various link states in a specific order in this project: link, visited, hover, active. In general, maintaining this order is critical because changing it causes link styles to stop working. The order of LVHA has a few mnemonics you can use: "LoVe-HA!" and "Like Various Hairy Apes" are two particularly memorable ones.

A Touch of Cleanup

If you look closely at the text above and below the jazzed-up links, you can see that it comes very close to the borders of the links. This is because when you set a border on an inline element (such as a hyperlink) and then give it some top and bottom padding, the border will get pushed into other lines of text. The lines won't get pushed apart. If you set the padding large enough, the box will start overlapping other lines or being overlapped by them.

Given this fact, and also seeing that the paragraphs are snuggling up to the edges of the table cell, let's give it some margins and increase the height of the text lines:

td#main {background: #FFD; color: black;
  border: 2px solid #797; border-width: 2px 2px 2px 1px;}
td#main p {margin: 0.75em 1.5em; line-height: 1.33em;}
td#navbuttons a {display: block; margin: 0;}
td#navbuttons img {display: block; height: 50px; width: 50px;
  border: 1px solid #ACA; border-width: 5px 10px;
  background: transparent;}

With this last change, we're ready to dazzle the client with our new design! The complete style sheet is shown in Listing 4.1, and the result is shown in Figure 4.13.

Figure 4.13Figure 4.13 Making the text a little easier on the eyes.

Listing 4.1 The Complete Style Sheet

<style type="text/css">
body {background: #CEC; color: black;}
h1 {margin-bottom: -0.25em;
  font: bold 200% Arial, sans-serif; color: #797;}
table#inform td {vertical-align: top; border-top: 3px solid #797;}
td#navbuttons {background: #ACA; padding: 0;
  border: 2px solid #797; border-width: 2px 1px 2px 2px;
  text-align: center;}
td#main {background: #FFD; color: black;
  border: 2px solid #797; border-width: 2px 2px 2px 1px;}
td#main p {margin: 0.75em 1.5em; line-height: 1.33em;}
td#navbuttons a {display: block; margin: 0;}
td#navbuttons img {display: block; height: 50px; width: 50px;
  border: 1px solid #ACA; border-width: 5px 10px;
  background: transparent;}
td#navbuttons img#gas {border-color: #797; background: #797;}
td#navbuttons a:hover {background-color: yellow;}
td#navbuttons a:hover img {border-color: yellow;}
td#navbuttons a:active img {border-color: #FC3;
  border-style: inset;}
a:link, a:visited {background-color: transparent; font-weight: bold;}
a:link {color: #171;}
a:visited {color: #747;}
a:visited:hover {color: #FFD; background-color: #747;}
a:link:hover {color: #FFD; background-color: #797;}
a.help:link, a.help:visited {padding: 0 2px 1px 16px;
  background: #FDD url(help-icon.gif) left center no-repeat;
  color: #733; border: 1px solid #C66;
  text-decoration: none;}
a.pr:link, a.pr:visited {padding: 0 2px 1px 16px;
  background: #EEC url(pr-icon.gif) left center no-repeat; 
  color: #171; border: 1px solid #797;
  text-decoration: none;}
a.help:visited {color: #A88; background-color: #EDD; 
  background-image: url(help-vicon.gif);}
a.pr:visited {color: #797; background-color: #DDC; 
  background-image: url(pr-vicon.gif);}
a.help:hover {color: #FFD; background-color: #C66;}
a.pr:hover {color: #FFD; background-color: #797;}
</style>
  • + Share This
  • 🔖 Save To Your Account