Skip to the content...

Welcome to the David Walsh Blog. I'm a MooTools, Dojo, jQuery, CSS, and PHP Web Developer located in Madison, Wisconsin, United States. Please contact me if I can make your experience on my website better.

Creating & Using CSS Sprites

22 Responses »

The idea of CSS sprites is pretty genius. For those of you who don't know the idea of a sprite, a sprite is basically multiple graphics compiled into one image. The advantages of using sprites are:

  1. Fewer images for the browser to download, which means fewer requests to the server.
  2. Total images size is generally smaller, so less download time for the user and less bandwidth consumption.
  3. No ugly mouseover code. No JavaScript -- only CSS!

For this example, I've used the DW illustration. I have a color version and a grayscale version which I have merged into one file as you can see below.

DW!

Now, it's time to see how the system works.

The HTML

	<a href="http://domain.com" id="logo-link"> </a>

Just a very simple anchor tag with a unique ID.

The CSS

#logo-link
{
	width:191px;
	height:151px;
	text-decoration:none;
	display:block;
	background-image:url(dw-logo-sprite.jpg);
	background-position:191px 0;
}
#logo-link:hover,#logo-link:active	{ background-position:0 0; }

When the image initially loads, I want the grayscale version, which is on the right site. For this reason, I set the link's initial background position 191 pixels to the left. When someone mouseovers the link, however, I want to show the color version. It's only then that I remove the 191 pixels.

Wanna see the working version?

Discussion

  1. June 3, 2008 @ 12:29 pm

    I have known about this for a long time but I never knew it was called sprites. :)

  2. June 3, 2008 @ 12:33 pm

    Another interesting side effect is that you can’t copy the image (in IE at least) and this gives you some image protection (for newbies that is).

  3. June 3, 2008 @ 3:12 pm

    Hey David, I’m curious as to what this does? href=”javascript:;”

  4. June 3, 2008 @ 3:39 pm

    It’s cool. Any tools can create the sprite images other than the expensive PS?

  5. June 3, 2008 @ 4:18 pm

    @Jesus: “javascript:;” tells the link to do nothing when clicked. I think another method is going “void(0);”.

    @NYC Guy: I recommend GIMP. Free, but not as powerful as Photoshop.

  6. June 4, 2008 @ 7:54 am

    Good points, but make it more generally

  7. rob simpkins
    June 4, 2008 @ 7:57 am

    NYC Guy, I have found inkscape to be a useful tool instead of photoshop, and it is free to download online too :)

    http://www.inkscape.org.

  8. June 4, 2008 @ 3:20 pm

    I actually wrote a similar article last week explaining how to create the image, and the HTML/CSS for this for navigation images. Also, I explained how to have the text input there so it’s not jsut a blank tag so in case CSS is disabled (or background images – like if you are on a blackberry) a link will appear in it’s place.

    I really dig your font – is Cambria cross-browser/cross platform compatible?

  9. June 4, 2008 @ 3:25 pm

    @Lindsey: Awesome article — I hadn’t seen that. The text-indent is genius. Great addition!

    As far as Cambria, every browser I’ve checked my site in has it. I’m not a Mac guy, but I’m pretty sure it works there too.

  10. ryan
    June 18, 2008 @ 3:23 am

    I don’t know what browser support is like for this (I’ve checked it in IE7 and Firefox5), but by the W3 recommendation for CSS 1 this should work:

    a.rollovers{
    background-position: 100% 0;
    }

    a.rollovers:hover, a.rollovers:focus, a.rollovers:active{
    background-position: 0 0;
    }

    I’m a fanatic about improving programming style. Mostly because I’m lazy. If this helps, consider it my way of saying thanks for a post that resolved my debate between keeping style in the stylesheet or avoiding ugly background flash.

  11. ryan
    June 18, 2008 @ 5:07 am

    …ahem. Firefox 2, I mean. God knows where the 5 came from.

  12. September 8, 2008 @ 7:00 am

    How about adding some mootools magic to the effect?

    Similar to what they did with JQuery here
    http://alistapart.com/articles/sprites2

  13. September 8, 2008 @ 7:47 am

    @Paul: Thanks for the suggestion!

  14. September 17, 2009 @ 6:10 pm

    hi david, thx for this tut.. i use this for my navigation.

  15. sven
    September 20, 2009 @ 6:41 pm

    Thanks for sharing this.

    I am not good with css so I am hoping for some help with this “problem”:

    When I use the same css as you do and wants to line up some images (“share icons” and Smilies” with no hover) I get theme below each other and not in a line.

    I have tried to add the classic “nobr” tag and the css “white-space: nowrap;” – but still it breaks.

    Also: when I use this for share this icons and “smilies”, should I add the “background image” for each of the icons or is there a better way?

    I am sure this is a small problem for you css experts, but please give med some feedback on this.

    Thanks :)


    a.shock {
    background-image: url('css-spirite.gif');
    background-repeat: no-repeat;
    background-position: -40px -223px;
    width: 15px;
    height: 15px;
    display: block;
    overflow: hidden;
    }
    a.wink {
    background-image: url('css-spirite.gif');
    background-repeat: no-repeat;
    background-position: -116px -223px;
    width: 15px;
    height: 15px;
    display: block;
    overflow: hidden;
    }

  16. September 22, 2009 @ 4:23 am

    If I get your problem right you want to display links with icons all in one line? If so you defined display:block on the a – this means it is now a block element – not inline anymore. Block elements get a 100% width and always fall one under another.

    To solve this add float:left; to your links and all of your a tags will float to the left part of the containing div. Also don’t forget to clear them afterwards or set overflow:hidden on the container element so elements below won’t fall under links.

    Also avoid adding html elements just to solve design issues.

  17. sven
    September 22, 2009 @ 5:34 am

    @easwee: Thanks for your reply.

    Well, I am learning more and more about CSS, like “inline” and “block”. Problem solved, thanks again :)

  18. October 11, 2009 @ 9:50 am

    There is a free tool to create CSS Sprites automatically – http://sprites.in/ .
    If you are interested in website speedup you can also try Web Optimizer application (also as a WordPress or Joomla plugin) here – http://www.web-optimizer.us/

  19. October 16, 2009 @ 8:22 pm
  20. January 12, 2010 @ 5:40 am

    nice and have written article about your post on my blog

  21. david
    April 20, 2010 @ 11:36 am

    This is great i am going to name my first born son after you. :) Thanks for the info this helped a lot and it was easy to understand.

  22. June 29, 2010 @ 5:12 am

    This is another one of those things you Google and get endless useless demos that are over complicated and dont work in IE.

    You example is straight to the point and works cross browser.

    Thanks for that

Be Heard!

Share your thoughts with fellow developers of all skill levels! I want to hear from you!

Name*:
Email*:
Website:  
Wrap your code with <code> tags, f00!