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

css: preloading images with css

Now excuse me for pulling up an old chestnut, but this is one that comes up often for people who are just starting out with CSS and want to avoid the extensive javascript output by many website design applications such as Dreamweaver and the dreaded Front Page. This method only requires a very minor understanding of CSS, cleans up your markup and achieves the desired effect.

Step 1: Creating the CSS.

First, let’s start with the CSS we’ll need. This step is very simple, as there is only one line that you will need to add to your stylesheet. Decide what name you would like to use for your preload. In this case we have gone with the creative Class name “preload.” If this is fine for you, simply paste this code into your stylesheet. Otherwise, change the Class to another name you are more comfortable with, bearing in mind you will need to do this on the final code as well. I’ll cover why we chose to use a Class instead of an ID in just a minute.

Code to paste into your stylesheet:
.preload{display:none;}

Step 2: Creating the DIV.

Next, we’ll create the actual DIV that will contain the images you want to preload. If you are using a site that you created yourself, this can go in the footer area of your page, as this will ensure that the main portion of the site loads before your preloaded images. If you want this preload active on every page of your site, make sure to either A. Copy the code to each page of your site, or B. (Preferred) include a footer.txt file that will contain this preload on every page of your site. That way, if you need to add or remove an item, you can do it in one place instead of on every page. If you are using a site that has a WYSIWYG editor and you only want the preload on one page (or want to add different images for preload on each page), you can do this by clicking on the “view code” or “view source” button in the editor and entering the code at the bottom of the editor. The reason we chose to use a Class rather than an ID is because this will grant you the most flexibility to add more than one set of preloads to a site. Say you have a web site with database-driven content. You have a set of preload images in the footer that is outputted automatically on every page, but one particular page also needs a few more preloaded images. You can accomplish this simply by adding those few images into the WYSIWYG editor as described above, without having to worry about having two DIV’s with the same ID on a particular page.

Code to modify/paste into your web site:
<div class="preload">
<img src="images/dog-over.gif" alt="Dog" />
<img src="images/cat-over.gif" alt="Cat" />
<img src="images/mouse-over.gif" alt="Mouse" />
</div>
Remember to add an alternate tag to each image!

Step 3: Testing.
Try it out! For our example, we created two different sets of images. One has a CSS preload, while one does not. Run your mouse over each, see the difference? The preloaded image loads immediately, while the non-preloaded image flashes before loading. Note that once you have moused over the non-preload image once, you will need to refresh the page to see it flash again.

WITHOUT Preload

WITH Preload


The DynamiX Mascot

Compatibility:
This method has been tested in and is compatible with Internet Explorer 5.5, 6, 7, Firefox (PC, Linux 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: ,

8 Responses to “css: preloading images with css”

  1. CSSnewbieNo Gravatar Says:

    A coworker and I were wrestling with this exact problem earlier today. We needed our images to pre-load before we applied some AJAX-y goodness, but didn’t really want to add yet another layer of JavaScript to the page. We weren’t sure if the browser would bother to download a hidden (non-displayed) image. This article was exactly the clarification we needed. Thanks!

  2. DynamiXLabsNo Gravatar Says:

    Hi CSSnewbie. I’m glad we were able to help! If you get stuck anywhere else along the way, send us an e-mail or leave a comment about what you are experiencing, and we will write an article solution for you. Thanks for the kind words!

  3. TheDrawing1No Gravatar Says:

    I just stumbled on your article. However, herewith you may consider your preload method compatible to Linux Firefox as well.

  4. DynamiXLabsNo Gravatar Says:

    Thanks for the heads up! I have updated our compatibility section.

  5. HostingNo Gravatar Says:

    Thanks for the knowledge.

  6. MartyNo Gravatar Says:

    Thanks for this tip, it has been a great help. I have a question, though. If you have used the same preload instructions on multiple pages, and the images get preloaded on the first page opened, does the browser follow that instruction and preload those images again when you go to the next page that also has the preload instructions, or, does it recognize that those images were already loaded?

  7. DynamiXLabsNo Gravatar Says:

    Hi Marty! We are glad this has been useful to you. In terms of the preload, the images are loaded by the browser on every page that calls them. From our testing, and it could depend on your browser settings and version, you will need to preload appropriate images on any page you need them. In other words, if you remove a preload call for an image that’s used on mouseover on a particular page, even if it has been loaded on another page, it will likely flash while it reloads. This is why we recommend using a class rather than an ID, because it will allow you to preload appropriate images on every page, while still creating preloads for individual pages as well. Hope this helps!

  8. JerryNo Gravatar Says:

    Works great for me! Awesome. Thanks!

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!