page splitter

Hollen-B Web and Graphic Design Portfolio

Tutorials

A Cool CSS Technique I Found being Used by Digg.com

How to display a single icon from a gif image with multiple icons. This CSS technique can be found being used by the designers of Digg.com.

| More
Share with friends and family

What I found on Digg

While working on one of my projects I wanted to add a star icon to symbolize add to favorites. Digg has a very similar option so I thought why not see how they handle this task. While digging through their source I decided to take a look at their icon image and come to find a long row of icons. Holy crow, what a fantastic idea! I have used similar techniques for rollover buttons but never a full on tool-set.

Link to Digg's icon tool set: http://cotnet.diggstatic.com/img/tools.gif

So why have a long strip of icons?

Well being that there is a total of 9 icons in the digg tool set and each icon is used all over the web site. Also depending on the user criteria some icons may need to be displayed in a different color to show the user has already clicked the icon. I'm sure we all can agree when you load an image once, the second time you load it will be even faster. So with that in mind, loading one image instead of multiple images sounds like a good idea to me. A few more good reasons to keep all your icons on one image would be less files to gather when reusing for other projects, less clutter in your image folders, cut time on tracking down icons, and why not keep all our tools in one object.

How the Digg tool strip of icons is used

In their CSS if you search for comments you will find
  1.  
  2. .comments {
  3. background: url(/img/tools.gif) no-repeat 0 -196px;
  4. }
  5. .comments, .share, .bury-link, .fave, .faved {
  6. padding: 4px 6px 4px 18px;
  7. text-decoration: none;
  8. }

What this does is takes their /img/tools.gif background image and moves it up 196 pixels which displays the comments icon in their tool set. The second set of classes use padding to position the text so it doesn't cover the background image. They also use padding to align text with the background image for proper placement. These classes style their href elements as seen below.

  1. <a href="#" class="tool comments">Comments</a>

Simple example coded by Brandon Hollenbeck

I coded this example below for the use of other people to play around with. Please feel free to use this tool set image created by me, just please no hot-linking.   tool_kit.gif

CSS

  1. #tool_bar {
  2. float:left;
  3. margin:0;
  4. padding:0;
  5. border:1px #CCCCCC solid;
  6. }
  7.  
  8. #tool_bar li {
  9. float:left;
  10. list-style: none;
  11. border-right:1px #CCCCCC solid;
  12. }
  13.  
  14. #tool_bar .no_bdr { border:none; }
  15.  
  16. .save, .saved, .delete, .comment, .hover_save {
  17. padding: 4px 6px 4px 20px;
  18. font-size: 16px;
  19. text-decoration: none;
  20. }
  21. .save { background: url(images/tool_kit.gif) no-repeat 2px 5px;}
  22. .saved { background: url(images/tool_kit.gif) no-repeat 2px -35px;}
  23. .delete { background: url(images/tool_kit.gif) no-repeat 2px -75px;}
  24. .comment { background: url(images/tool_kit.gif) no-repeat 2px -115px;}
  25. .hover_save { background: url(images/tool_kit.gif) no-repeat 2px 5px;}
  26. a:hover.hover_save { background: url(images/tool_kit.gif) no-repeat 2px -35px;}
  27. }

 

HTML

  1. <ul id="tool_bar">
  2. <li><a href="#" class="save">Save</a></li>
  3. <li><a href="#" class="saved">Saved</a></li>
  4. <li><a href="#" class="delete">Delete</a></li>
  5. <li><a href="#" class="comment">Comment</a></li>
  6. <li class="no_bdr"><a href="#" class="hover_save">Save/Saved Rollover</a></li>
  7. </ul>

 

Example

| More
Share with friends and family

Comments

Martin MarusaSumited: 05/25/2010
Hi,nice tutorial, but I think that css sprites are used to load your image only once for the part and than just move it around as needed.So maybe "#toolbar a" should contain "{background-image:tool_kit.gif,background-repeat:no-repeat}" and than the classes ".save{background-position:2px 5px}" and so on...
JamesSumited: 05/25/2010
that is a pretty old technique. Called CSS Sprites.
DimmyyySumited: 05/06/2010
Great tutorial. Thanks!
AbhisekSumited: 04/02/2010
Very nice tutorial. The technique is called CSS Sprites. Not only Digg, most websites with high volume of traffic use this technique. Facebook and Gmail use it too. :-)
Brandon CarlSumited: 03/14/2010
Excellent Tutorial!I've always wondered how big sites consolidate their pages to load so fast, and I guess they pretty much all use these sprite images.Look at the one for the reCaptcha below: http://api.recaptcha.net/img/red/sprite.pngThat's some hard-core CSS. :)Thanks for the tut!
Add Comment