Using Fewer Images

August 27th, 2006

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 background 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.

16 Comments

  1. Julian BH

    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. hiredgun

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

  3. Bob Lindabury

    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.

  4. lawrence

    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

  5. JAson

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

  6. noclegi

    cool

  7. Free Blog Templates

    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

  8. jessie

    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. :-)

  9. bogota

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

  10. Damjan Mozetič

    Nicely explained, congrats.

  11. kevin

    is there a way for it to display inline?
    when i change block to inline, it does not work
    is there a way around this?

  12. PatDaniels

    Wow, thanks a lot! Decreased my file sizes by 118 kb using the single image version. :D

  13. ad3ck

    am i a lil late here? nevermind. but here i’ve found what i’m looking for. thanx for the useful tutorial man. ;)

  14. Andy Patmore

    Better to be late than never! This is a gem of an idea. Not totally foolproof as lawrence points out, but very useful none the less!

  15. ad3ck

    Hello again. I created more than one image (here: eminelle.com) but it doesn’t work if viewed with Internet Explorer (IE). Do you know how to solve it?
    Thank you.

  16. css

    Cascading Style Sheets (CSS) web design lessons
    Css link Properties Attributes - examles

    http://css-lessons.ucoz.com/link-css-examples-1.htm
    http://css-lessons.ucoz.com/link-css-examples-2.htm

Add a Comment

One Trackback

  1. wagthis.com

    magnetiq.com » Blog Archive » Using Fewer Images…

11 Pingbacks

  1. Graywolf’s Delicious Blog » Blog Archive » links for 2006-10-09

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

  2. TUGGO » Blog Archive » Using Fewer Images

    [...] 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. [...]

  3. Free CSS Editors : lxpages.com blog

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

  4. Web Design/Programming « The Caera Kirsch Collection

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

  5. Aprendiendo Hojas de Estilo (CSS) « Disenia

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

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

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

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

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

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

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

  9. Bleebot | Christophe Lefevre » 101 astuces CSS, tutos et examples

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

  10. All in a days work…

    [...] 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. [...]

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

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