Your Ad Here

Sliding Doors with CSS Sprites in harmony.

More CSS Sprites. This time combined with the 'sliding doors' technique. I got lots of inspiration from the guys at A List Apart for this, but I thought their examples to be a little more wordy than I really wanted, so I thought I'd write my own. Here's the working sample of this technique, with my own ugly buttons.

Let's start with the images themselves.

Here's 'left.gif':

And 'right.gif':

Now for the CSS:

HTML:
  1. body {
  2. background:#fff;
  3. margin:0;
  4. padding:0;
  5. color:#000;
  6. font:x-small/1.5em Georgia,Serif;
  7. voice-family: "\"}\""; voice-family:inherit;font-size:small;
  8. }
  9. html>body {font-size:small;}

The only thing worth noting here is the focus (with 2 different hacks) on controlling the font-size for the links. This is addressed at ALA, so I'll leave it out here. Suffice it to say that the size of the fonts is pretty important to making all the other measurements you'll be making with these buttons.

HTML:
  1. #header {
  2. float:left;
  3. font-size:100%;
  4. line-height:normal;
  5. }
  6. #header ul {
  7. margin:0;
  8. padding:10px 10px 0;
  9. list-style:none;
  10. }

Next, we setup the basic settings for the list itself. We want the font size to adhere to what we set for the body, and we want the list itself to only be an html container mechanism, so we get rid of the margin and list-style in "ul".

HTML:
  1. #header li {
  2. float:left;
  3. background:url("left.gif") no-repeat 0 -40px;
  4. /* set the value here to the space you want between the buttons */
  5. margin:2px;
  6. /* set the last value to the exact width of the left.gif image */
  7. padding:0 0 0 6px;
  8. }
  9. #header a {
  10. float:left;
  11. display:block;
  12. width:.1em;
  13. background:url("right.gif") no-repeat 100% -40px;
  14. padding:12px 26px 13px 20px;
  15. text-decoration:none;
  16. font-weight:bold;
  17. color:#444;
  18. }

Here's where the first bit of magic comes into play. We set the background image of the list item itself to be the left sprite, and the background image of the link (which lives inside the list item) to be the right side. The thing that makes the right side show it's rounded corner is our 100% setting in the background of the "a" style. This ensures that no matter how long or short the content of the a tag, the right side will always be the far right side of the right image. In other words, it sort of 'snaps' the right side of the background image to the right side of the link text.

Play around with the padding settings for each of these to see what they do, but it's worth noting that they will change for you in direct relation to the size of your background image and font, respectively.

HTML:
  1. #header> ul a {width:auto;}
  2. /* Commented Backslash Hack hides rule from IE5-Mac \*/
  3. #header a {float:none;}
  4. /* End IE5-Mac hack */
  5. #header a:hover {
  6. color:#444;
  7. }

Once again, these IE hacks control the presentation of the links themselves, and are explained in more detail at ALA.

HTML:
  1. li#current {
  2. background-position:0% 0;
  3. border-width:0;
  4. }
  5. li#current a {
  6. background-position:100% 0;
  7. padding-bottom:13px;
  8. color:#339;
  9. }

Although ALA ends up using Descending Selectors for their final version, I chose not to for mine, and stuck with a 'current' tag. I figure it's less CSS, and if my dynamic pages know the name of the current page, they can throw the current tag in just as easily with a simple loop. It's a matter of preference, from what I can tell, but you can make that call yourself.

However, the thing worth noting here is that the background-position property is the only thing we're messing with here. This is where our CSS Sprite magic comes into play. Now we're working with rounded corners, variable widths, minimal code, and minimal http requests with only 2 images!!!

HTML:
  1. #header li:hover, #header li:hover a {
  2. background-position:0% 0;
  3. }
  4. #header li:hover a {
  5. background-position:100% 0;
  6. }

This last bit just sets the hover properties for both the list item and the link. It's worth noting that this works because the "a" tag is nested inside the "li" tag. And, since the "a" tag is set to display:block, when you rollover one, you're also rolling over the other, thus triggering both hover properties at once.

HTML:
  1. <div id="header">
  2. <ul>
  3. <li><a href="#">Home</a></li>
  4. <li><a href="#">News</a></li>
  5. <li id="current"><a href="#">Products</a></li>
  6. <li><a href="#">About</a></li>
  7. <li><a href="#">Contact</a></li>
  8. </ul>
  9. </div>

Finally, our html body contains a div with the id we've handled in the css, and an unordered list that contains a 'current' id somewhere in the list.

Leave a comment

Please be polite and on topic. Your e-mail will never be published.