Visit DynamiX Web Design! E-Mail DynamiX Web Design!

css: add a “loading” icon to your larger images

Let’s say that you have a large image (or several large images) on a particular page, and you want to let visitors know that the image is loading. You could use a piece of javascript to embed a “loading” image for all images that have not yet loaded, but unless you are loading a ton of very large images on a single page, we have a much simpler (and cleaner) method to accomplish the same thing.

Step 1: Find a preload image.

There are many fantastic sites out there that will allow you to create your own preload images. Our favorite is here. Just remember not to choose an icon that is too large (file size), or it may not load until after the larger image has loaded. Here is the one that we have created:

loading image

Step 2: Create the CSS

There is just a little bit of code to create here, and it can be pasted directly into your stylesheet. Just be sure to replace the “youricon.gif” text with your image name.

Code to paste into your stylesheet:
.load{background:url('images/youricon.gif') no-repeat center;}

Step 3: Applying the code
There are a couple of ways to do this, but this is probably the best solution (depending on your application). Simply encase the image you are interested in loading within a “load” div, and apply the width and height of your image to the div.

Example:
<div class="load" style="width:200px;height:200px">
<img src="large.jpg" alt="alternate text" /></div>

Here it is applied to an image:
You will likely need to clear your cache and reload this page to see the loading icon appear. A more thorough example of this loading icon in action can be found here.

a very large image

As mentioned by contributor Lim Chee Aun below, you could also bypass the div and apply the .load class directly to the image itself, and here’s how you can do it:
<img src="large.jpg" alt="alternate text" class="load" style="width:200px;height:200px" /></div>The reason we hadn’t mentioned this idea initially, is if the image does not load it can break the look of the design. If you are absolutely positive you will not have any loading issues, you don’t care if the image needs to be centered (while validating xhtml strict), or if you are applying the class to multiple images, this may be a cleaner solution for you. See our updated example page below for examples of the method created both ways.

If this solution doesn’t fix the problem for you application, let us know what you are doing differently and we’ll be happy to go into more detail.

Compatibility:
This method has been tested in and is compatible with Internet Explorer 5.5, 6, 7, Firefox (PC and Mac), Netscape and Safari (PC and Mac).

That’s all folks!

If you have an idea or article that you would like to contribute, send it on! We’re always looking for good, quality articles. Note that we will not republish an article that has been published elsewhere, so keep it original!

del.icio.us Reddit Slashdot Digg Facebook Technorati Google StumbleUpon Furl Netscape Yahoo BlinkList Feed Me Links Bloglines Ask Mister Wong Newsvine Wink Squidoo Fark Blogmarks Sphere

Tags: , ,

24 Responses to “css: add a “loading” icon to your larger images”

  1. Lim Chee AunNo Gravatar Says:

    If I’m not mistaken, if the width and height are specified for the img tag, the ‘.load’ class or the background loading image could be applied directly to the img tag instead of the additional div tag.

  2. DynamiXLabsNo Gravatar Says:

    Thanks for the suggestion Lim Chee Aun! We have updated our article and example page to reflect this option.

  3. Richard@HomeNo Gravatar Says:

    Brilliant, and simple!

  4. links for 2008-01-28 « Richard@Home Says:

    […] DynamiX Labs » articles » css: add a “loading” icon to your larger images What a good idea. Might be a bit much for every image on your page, but would work like a charm for something like a gallery. (tags: css loading animation image graphics) […]

  5. User First Web » links for 2008-01-28 Says:

    […] DynamiX Labs » articles » css: add a “loading” icon to your larger images One of those “oh duh!” idea. Put a loading image in the background so that it is covered when the real image loads (tags: image css speedupyoursite) […]

  6. DynamiXLabsNo Gravatar Says:

    Hi Richard. Exactly right, it’s ideal for either a gallery, or for images that are considered very large. In other words, the 15kb jpg’s on the site won’t need the effect as they will load too quickly, but a 150kb image could benefit.

  7. Skylog » Blog Archive » links for 2008-01-29 Says:

    David Scatigna has linked to this article.

  8. Freedom of Speech Says:

    Freedom of Speech has linked to this article.

  9. CSSnewbieNo Gravatar Says:

    This is a pretty cool idea!

    However, wouldn’t it be easiest overall to just apply the background image to the <img> tag directly, without having to worry about a class? Something along the lines of:

    img {background:url(’images/youricon.gif’) no-repeat center;}

    Admittedly, most images would load quickly enough that you’d never see the background image. But it saves you (the designer) the problem of deciding what constitutes a “large” image. What may be sufficiently small for someone on broadband might be quite large for someone stuck using dial-up. And even broadband hiccups from time to time. At least then, the user knows something is loading in that space.

    Again, great idea!

  10. DynamiXLabsNo Gravatar Says:

    Hi CSSnewbie. That’s a great point, and the reason we didn’t mention it is because it does tend to open up a lot of opportunities for unforeseen issues.

    For example, if you are using a javascript piece to rotate images on your web page, the loading icon will fade in and out behind the images. Now this may sound like an added bonus, but we have tested this idea and found that it is a detriment to the look of the rotating image effect.

    In addition, in at least half of the image rotators we’ve tested, the background “loading” image was plainly visible even when the primary image was fully faded in, which of course is a distraction and unattractive. Also take the possible additional (though likely minor) load time associated with rendering the loading icon in a myriad of places where it is not necessary, and the potential that you will load an improperly colored “loading” image in certain places.

    Say, for example your main content background area is white, but the left hand column is black. If you have created a loading gif with a white background, the loading image won’t fit with the left hand column area’s color scheme. Admittedly, you can create a loading icon with a transparent background, but this will also significantly add to the file size of the loading icon.

    Since there will likely be few instances where an image will be large enough to warrant a “loading” effect, we thought it simplest to just encase the one or two images that may be affected by a long loading time, and leave the rest be. Again, a great thought, and depending on your application this may indeed be the simplest solution for you.

  11. FloroskopNo Gravatar Says:

    Hello!
    I think this try.

  12. Pure CSS Effects and Tips (Does not use JavaScript) | DESIGNwalker max Says:

    Design Walker has linked to this article on their web site.

  13. Using CSS to Do Anything: 50+ Creative Examples and Tutorials Says:

    Noupe has linked to this article on their web site.

  14. Effets en Css pur et Conseils ( sans javascript ) Says:

    kawa Mag has linked to this article on their web site.

  15. Best Of April 2008 | Best of the Month | Smashing Magazine Says:

    Smashing Magazine has linked to this article on their web site.

  16. Richard ShieldsNo Gravatar Says:

    Love you guys! We have updated our site’s portfolio which uses large images with this handy CSS: http://impresslab.com/work/web.

    Thanks! Keep it up!

  17. DynamiXLabsNo Gravatar Says:

    Thanks Richard! I’m glad one of our articles was of use to you. Great looking web site by the way!

  18. DavidNo Gravatar Says:

    Hi,
    I found this topic very interesting. I was wondering if this method would work for embedded swf files in an html page. As they are usually larger then images it would be a neat trick.

  19. DynamiXLabsNo Gravatar Says:

    Hi David,

    As long as you apply the div around the swf file, it should respond in the same fashion. The only thing to be aware of, is that unless you have specified that the swf should have a transparent background (by adding the wmode=”transparent” parameter), it may not work. Try it out, and if you get stuck, let us know and we can create an example for you. Thanks for the question!

  20. MJNo Gravatar Says:

    - I don’t think a period has to be added in .load. Isn’t it just “class=load” instead of “class=.load”?

  21. DynamiXLabsNo Gravatar Says:

    whoops! Looks like you found a typo. Thanks for letting us know! That has been changed.

  22. OnjefuNo Gravatar Says:

    This is an amazingly clever technique! Thanks for sharing!

  23. web designNo Gravatar Says:

    Thank for sharing with us. great tutorial

  24. danrebNo Gravatar Says:

    Great!, I will add this technique in my website thanks for this great tutorials.

We want your feedback! Leave us your thoughts below.



Interested in hiring the DynamiX Web Design team for one of your projects? Give us a ring!
Add to Technorati Favorites

Copyright © 2008 DynamiX Web Design, LLC.  |  DynamiX Labs is proudly powered by WordPress

Entries (RSS) & Comments (RSS).  |  thanks & about.
The DynamiX Mascot button submit it!
hire dynamix!