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

Home > Articles > Web Design & Development > CSS

  • Print
  • + Share This
From the author of

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 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 3Figure 3 The first step in re-creating the basic design.

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;}

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). 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 4Figure 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 other styles 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 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 5Figure 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 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;}

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

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.

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 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>

Figure 7Figure 7 Highlighting an icon with borders and background.

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

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>

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 8).

Figure 8Figure 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 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.

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>

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 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 9Figure 9 When changing the appearance of links, it's best to make sure they still stand out.

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.

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 10). Oh, and just for the heck of it, we'll eliminate the underline, too.

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>

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

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).

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 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 11Figure 11 Now there are two way-cool links for our viewing pleasure.

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 (see Figure 12). 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;}

Figure 12Figure 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.

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 1, and the result is shown in Figure 13.

Listing 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>

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

  • + Share This
  • 🔖 Save To Your Account