Using Fewer Images

Image rollovers are usually composed of two individual images; one for the default state and one for when the mouse is hovered over the image or link. However, it bears some advantages to use a single image by taking advantage of CSS image offsets. Here’s how a rollover for an anchor is usually done:

The HTML code could look like something like this:

<a href="#" id="yaprak">&nbsp;</a>

The associated CSS would then have items like:

a#yaprak {
    width: 64px;
    height: 64px;
    background-image: url(yaprak_bw.png);
    display: block;
    text-decoration: none;
}

a#yaprak:hover {
    background-image: url(yaprak_color.png);
}

Two individual images for the two states would have to be uploaded to the server and they would look like this:

Finally, the end result would be:

The default image that is visible to the user is the black & white yaprak_bw.png and when you hover over the image, it’s replaced by yaprak_color.png with its full-color glory. However, if this is a user’s initial visit to the website and therefore the color image hasn’t already been cached, there may be a user-perceivable delay in switching to the color image. The net annoyance will depend on factors like the user’s connection speed, the load on the web server and most importantly, the attentiveness of the user. A couple of methods can be applied to pre-load alternate images to avoid the perceived latency but I will advocate for something else: don’t use separate alternate images in the first place. You can use a single image where the two rollover images are flush with eachother, side by side or one on top of another. Here’s how:

The HTML code is the same. The CSS this time has:

a#yaprak {
    width: 64px;
    height: 64px;
    background-image: url(yaprak_bw_color.png);
    display: block;
    text-decoration: none;
}

a#yaprak:hover {
    background-position: 64px 0;
}

There’s only a single image:

The end result is:

Using fewer images might marginally speed up the load times for your website while also decreasing the load on your web server. Another scant advantage is that you’d have less images to worry about maintaining and uploading to your web server.

25 Responses to “Using Fewer Images”

  1. Julian BH Says:

    This is really cool. I’ve seen this written about before, but seeing it here, with such a simple explanation, finally made me do it. Thanks.

  2. wagthis.com Says:

    magnetiq.com » Blog Archive » Using Fewer Images…

  3. Graywolf’s Delicious Blog » Blog Archive » links for 2006-10-09 Says:

    […] magnetiq.com » Blog Archive » Using Fewer Images image rollover trick with only one image (tags: css webdesign) […]

  4. TUGGO » Blog Archive » Using Fewer Images Says:

    […] I recently found Ates Goral’s article on how to use less images on your webpages. His article highlighted how to use the background-position attribute in CSS when you mouseover an image to change the position of the background image. This forces site visitors to only have to load a single background image and CSS will just change the position. When you use his method you will still have to create two images. One will be in black and white, and the other in full color. […]

  5. Free CSS Editors : lxpages.com blog Says:

    […] CSS Trick: Using Fewer Images […]

  6. Web Design/Programming « The Caera Kirsch Collection Says:

    […] Using Fewer Images with Image Rollovers In CSS […]

  7. hiredgun Says:

    ah ah az mi sprite bob kaydirdik
    simdi webde kaydiririz nedir ki…
    kaydir.

  8. Aprendiendo Hojas de Estilo (CSS) « Disenia Says:

    […] CSS Trick: Using Fewer Images […]

  9. Оптимизаци?: и?пользуем одно изображение вме?то двух &laquo Свобода ?лова вебма?тер?кого Says:

    […] ?ашел в ?ети ??ылку на по?т буржуй?кого блога, опубликовавшего заметку “Using Fewer Images? (и?пользование меньшего количе?тва изображений). […]

  10. Web 2.0 Announcer Says:

    Using Less Images…

    […][…]…

  11. Web 2.0 Announcer Says:

    Using Less Images…

    […]Image rollovers are usually composed of two individual images; one for the default state and one for when the mouse is hovered over the image or link. However, it bears some advantages to use a single image by taking advantage of CSS image offsets…

  12. Web 2.0 Announcer Says:

    magnetiq.com » Blog Archive » Using Fewer Images…

    […][…]…

  13. 101 CSS Tips, Tutorials and Examples : lxpages.com blog Says:

    […] CSS Trick: Using Fewer Images […]

  14. Más tutoriales, tips y ejemplos con CSS | Blog Vecindad Gráfica Diseño Gráfico Says:

    […] CSS Trick: Using Fewer Images […]

  15. Bleebot | Christophe Lefevre » 101 astuces CSS, tutos et examples Says:

    […] CSS Trick: Using Fewer Images […]

  16. All in a days work… Says:

    […] Using Fewer Images (for rollovers) Image rollovers are usually composed of 2 images; one for the default state and one for when the mouse is hovered over the image or link, it’s advantageous to use a single image by taking advantage of CSS image offsets, a trick for optimizing your website. […]

  17. Bob Lindabury Says:

    While this is a cool idea for mouseovers, it’s not limited to mouseovers.

    You can create a single image containing all your icons for a website (or specific part of a website) and use background-position offests (along with specific width, height attributes pertaining to the specific icon) and no-repeat to populate your site with icons.

    This way you have consolodated your graphics into image collages.

  18. lawrence Says:

    Great Article,…

    … and you seems to have provided the benefits of using a single image…
    However as a designer,… and dealing with optimizing images for the web daily,… I must say that this option is limiting,.. because I have to chose one format for both states which may not necessarily result in smaller file sizes… with 2 images for the respective states, I can now have each image saved in the format that generates the best results with the smallest possible file size:

    e.g. yaprak_bw could be in gif/jpg/png (whichever generates a smaller size)

    cheers

  19. Leo.锦州 - 周亮的博客 » Blog Archive » 101 个经典CSS技巧和实例 Says:

    […] CSS Trick: Using Fewer Images […]

  20. JAson Says:

    this is a great way to decrease the potential for javascript conflicts as well…thanks for the simple explanation

  21. noclegi Says:

    cool

  22. Free Blog Templates Says:

    Amazing technique. I changed one of my buttons as soon as I read your article because my button was noticeably slow to load on hover. Now it’s smooth running!

    ~David

  23. jessie Says:

    i’m a newbie blogger and i’m still learning stuffs like CSS. I’ve learned from your post. keep posting tutorials like this and people will come back to your site more often. :-)

  24. bogota Says:

    Great, i never saw a simple explanation of this technique.. Thanks

  25. Damjan Mozetič Says:

    Nicely explained, congrats.

Leave a Reply