January 01, 2002
Styling Buttons
As a Web designer, odds are that at some point you've used the classic links-down-the-side interface: A page title across the top, the content below that, and down the left side (usually) you put a column of buttons. This approach has been used by nearly everyone from CNN to Microsoft to, well, Web Review.
In some cases, you may have decided that you were better off using just plain text links, since they're easier to fix if there's a spelling error, not to mention easier to change or add. If you need a new button, all you have to do is type it in, not create it in something like Photoshop. And, of course, you don't have to worry about button length that way. With graphic buttons, they all have to be the same width, or else they start to look dumb. If you add a new button that's wider than the others, then you may have to go back and resize the others, and that's just a pain.
With CSS, however, you can get the best of both worlds...assuming you're willing to put a little thought into accounting for browsers that don't support CSS. By using the background properties of CSS, you can have buttons that scale to whatever size is needed, and have interesting decorations, all at the same time, like these:
Doing it right
For the moment, let's assume that all browsers fully-support CSS1, and explore the simplest way to get the effect we want. In this case, all we need to do is set up each button like so:
<tr> <td class="cap"><img src="buttoncap.gif"></td> <td class="text">Button #1</td> </tr>This causes the image ( buttoncap.gif; the rounded end of the button) to be placed in one cell, and the text label for the button in another -- both in the same row. The style classes are there for creating effects, which we'll see in a moment. Without CSS, and with the row's borders turned on, the above markup would look like this:
buttonbg.gif, we can place it on the right side of the text cell's background like this:
TD.text {background: white url(buttonbg.gif) top right no-repeat;}
...but that, unfortunately, will get us the following:
buttonbg.gif is about 17 pixels wide, we can create an interesting effect by setting the right padding of the text cell to 10px. This will make the text look as though it's hanging into the cap, so to speak. This requires the following styles, and gets the following
result:
TD.text {background: white url(buttonbg.gif) top right no-repeat;
padding-right: 10px;}
top right properties in the style sheet. That's fine as long as the browser supports the positioning of background elements. But if the browser (e.g., Navigator 4) doesn't support such positioning, you'll end up with something that looks like this instead:
TD.text {background: white url(buttonbg.gif) top right no-repeat;
padding-right: 10px;
padding-left: 20px;}
No (Browser) Pain, No GainNow that we've managed to work around everything, we find that life isn't quite so accommodating. For example, the problem we just solved for Navigator 4 will produce the following in Internet Explorer 4.5 for the Macintosh:TD.text {background: white url(buttonbg.gif) top right no-repeat;}
<tr>
<td class="cap"><img src="buttoncap.gif"></td>
<td class="text"><img src="text.gif"></td>
</tr>
As you can see, we've produced roughly the same effect, although we had to drop the text to do it. While this is less in the spirit of CSS, the advantage here is that the text styling for each of the buttons will be the same. Also, you end up with buttons which look okay in various browsers; for example, here is how they should look in Internet Explorer 4.x and Navigator 4.x, respectively:
While they're not exactly the same, at least neither one looks like a complete mistake.
This month's excursion into CSS has been interesting, to say the least, given that we started with a nice text-based effect and managed to end up with a heavily graphics-based solution with a reduced use of CSS. Unfortunately, browser incompatibilities will do that to you. In general, the main point here is that if you're going to use techniques like those described in this month's column, you need to think your approach through carefully, and make sure that browser bugs won't completely wreck what you're trying to accomplish. Previously in Sense of Style
Styling Buttons
|
|
||||||||||||||||||||||
|
|
|
|