A Way to Preload Images without JavaScript that is SO Much Better

Published Saturday, June 14, 2008 @ 2:01 pm • 11 Responses

Responding to my first attempt at preloading images without JavaScript, CSS-Guru David Bowman graces his audience with a most enlightening triage of comments.

Apparently, the image-preloading technique explained in the article is “major overkill” and “totally ridiculous.” Of course, I will be the first to admit that I am no expert in CSS, but I do enjoy sharing my discoveries and watching as people improve upon them. My first attempt at preloading images without JavaScript may indeed be “pretty crappy,” but it certainly works.

Fortunately, several weeks prior to Mr. Bowman’s dazzling performance, insightful reader Duarte helps the community by sharing a far more elegant solution using display: none;. Here is an example of its implementation:

Step 1 — Place this in your CSS file:

div#preload { display: none; }

Step 2 — Place this at the bottom of your (X)HTML document:

<div id="preload">
   <img src="http://domain.tld/image-01.png" width="1" height="1" alt="Image 01" />
   <img src="http://domain.tld/image-02.png" width="1" height="1" alt="Image 02" />
   <img src="http://domain.tld/image-03.png" width="1" height="1" alt="Image 03" />
</div>

Once in place, this code will ensure that your images are preloaded and available for use elsewhere in the document. Just remember to call the displayed images using the same path as the the preloaded images. After that, everything should work perfectly. Indeed, even CSS-Wizard David Bowman “tested it out” and agrees that “the problem is solved.” Thanks for the confirmation, David! ;)

Alternatively, to avoid the extra (X)HTML markup, you may simply add background images to existing elements that are either dimensionless or empty. For example, to preload three images, image_01.png, image_02.png, and image_03.png, locate three appropriate elements in your markup and place something like this into your CSS file:


#element_01 {
	background: url(path/image_01.png) no-repeat;
	display: none;
	}
#element_02 {
	background: url(path/image_02.png) no-repeat;
	display: none;
	}
#element_03 {
	background: url(path/image_03.png) no-repeat;
	display: none;
	}

..and that should do it! For more information on this technique, check out this article: Pure CSS: Better Image Preloading without JavaScript. And who knows, if we’re lucky, maybe Mr. Bowman will once again grace us with his presence! ;)


Dialogue

11 Responses Jump to comment form

1David Bowman

August 9, 2008 at 5:23 pm

Indeed, I’ve returned to comment once again.

First things first: I believe the word you were looking for is “triad,” not “triage.” It would only be a triage of comments if you were physically wounded by what I wrote, which is probably not the case, because you have a pretty forgiving attitude and you were humble enough not to delete my comments.

I just think there are too many bloggers in the world posting common sense techniques and thinking they found something new. And I suspect that this is just a tactic used to drive traffic to their sites. It just seems devious and wrong. Anybody with 10 seconds of CSS experience would have used “display: none” and not even bothered to blog about it. Maybe you did overlook it to begin with, but you seem like a pretty prolific blogger, so to me it just looked like a keyword-laden traffic-funnel post.

Anyway, in the interest of writing concise code, I really did think your original solution was overkill. You wrote a blog post about how Google’s homepage was terribly bloated. It might be bloated and inefficient, “but it works,” just like your first attempt at using CSS for preloading. Your “solution,” proportionally, just about as efficient as Google’s homepage vs. your XHTML version (i.e. about five times longer than necessary).

2Jeff Starr

August 10, 2008 at 7:22 am

Hi David, I appreciate the comment. I completely understand where you are coming from: the Web is polluted with a ton of garbage. I totally get the point, and continue to do my best to keep the content here at Perishable Press as “high-quality” as possible. I work very hard every day to produce content for this site and keep it running. Every cost is paid for entirely “out-of-pocket” — as you can see, I do not advertised on this site, and certainly derive no income from its existence. I have no interest in writing “keyword-laden traffic-funnel posts,” but rather try to learn as much as possible about my subject matter and post articles to share my discoveries. I may not be perfect, and I may make horrible and brainless mistakes from time to time, but I assure you that I am doing my best to represent myself as honestly, transparently, and sincerely as possible. This is the “real deal,” and will always be that way.
Regards,
Jeff

3Amos Wenger

August 18, 2008 at 5:10 am

This is addressed to David Bowman : I stumbled across Jeff’s page (first attempt) when I was searching a solution to this very simple problem : I had to preload images. So I created a div and styled it with display:none, and filled it with images. And then I fired Firefox 3 and oh magic it worked. When I went to test it with Opera, guess what ? Look, ma, no preloading :(. Seems that opera guys have over-optimized here. So the only practical and cross-browser solution (as sucky as it is), is effectively to move the div offscreen and leave it display:static. Mr Bowman, I suggest you to be less prompt to bash people in the head.

About accessibility and users having CSS disabled, I guess that.. if you have a webpage that requires image preloading, it is complex enough to “require” CSS to be enabled. It’s not as “bad” as to be browser-specific.

Finally, I’d like to congratulate Jeff for his website, looks great, loads fast, and has sound advice and intelligent+constructive comments.

4Jeff Starr

August 18, 2008 at 5:22 pm

Thank you for the kind comments, Amos — they are greatly appreciated :)

5Amos Wenger

August 19, 2008 at 1:10 pm

You’re welcome Jeff, and one more congrats for.. your ranking on Google. Funny enough, perishablepress.com came 2nd when I googled my name+surname just 3 hours after having posted here.

More on the "display:none = no preloading" issue, I’ve stumbled across a paragraph on the jqModal website (jquery plugin), which contains a more complete explanation and a (better?) workaround. See the “image caching” section at http://dev.iceburg.net/jquery/jqModal/ (no spam intended).

Also, apologies for repeating but to anyone reading that, if you’re having sound problems with Flash and display:none objects, read http://perishablepress.com/…/#comment-66277 and follow-ups.

6Jeff Starr

August 20, 2008 at 9:04 am

Hi Amos, that preloading method is interesting indeed. I will be looking into it further. As far as I can tell, the technique looks quite similar to the method described here, only with an explicit display:block declaration and the @import inclusion method. The block display property seems obvious (and redundant — divs are block elements to begin with), but the import directives are a mystery. i.e., why import the rules? What is the effect? Does it account for some of the preloading and Flash issues we’ve encountered? I need to check it.. ( thanks for pointing it out :)

7Amos Wenger

August 21, 2008 at 10:41 am

Hey Jeff, yes, the method is similar indeed, but the part that caught my eyes was the @media (and not @import, if of course we are talking about the same piece of css, and if my eyes aren’t faulty). I didn’t think about printing and this piece of code reminds me that.. moving images so far offscreen may cause serious printing problems, like many blank sheets and totally wrong layout. (Though it’s not exactly a piece of news that the web isn’t exactly printing-friendly).

Also I’d like to bring up an issue : if a page contains many high-resolution images to be loaded, the browser (tested, FF3/Linux and Opera 0.53/Linux) can stall while loading all the images. I was thinking that maybe a piece of javascript code could limit the number of simultaneous downloads, by writing progressively the “offscreen preloading divs” to the DOM, keeping progress by monitoring onload events on images. I don’t know if anyone is interested in that, though.

8Jeff Starr

August 31, 2008 at 8:02 am

Hi Amos, sorry for the delay — I am just now returning from a nice vacation and looking forward to getting back into things.

You are correct about the @media — that is what I meant to say (yes, we are looking at the same piece of code). I definitely like the idea of accounting for print stylesheets and will incorporate that method into the next relevant post.

I think the JavaScript solution you mention would be a worthwhile method for anyone doing a lot of heavy image preloading. Most of the preloading that I have been involved with deals with much less content, but that’s not to say that my next project won’t require preloading of that magnitude.

9Claudio

November 22, 2008 at 1:13 am

the best “preload images” option I’ve found!
TKS!

10kevin

December 2, 2008 at 2:00 pm

Addressed to David Bowman: Do you have any blog so that I could write some nice comments? may be i could learn some techniques from your blog, like how to get real traffic to make money using blogs, without letting people know you are making money???

Hehe, Just a joke. Don’t take it seriously.

Oh, I like this preloading method. Thanks for the post.

11Jeff Starr

December 3, 2008 at 3:20 pm

@kevin: My pleasure! Thanks for dropping by :)

Subscribe to comments on this post


Share your thoughts..

TopRead official comment policy

Contact Perishable Press

  • Contact Jeff via form

Search Perishable Press

About Perishable Press

Perishable Press is the virtual playground of Jeff Starr — visionary, founder and lead developer of Monzilla Media, a small web and graphic design company in the lush desert oasis of Moses Lake, Washington. Perishable Press features articles and tutorials on many aspects of digital design..

Read more..

Perishable on Twitter

better to have too many ideas and not enough time than the converse

Perishable on Tumblr

WordPress Tip for Multiple Themes

Sunday, 4 January 2009, 5:16 pm

If your site makes available multiple themes for users to choose from, remember to include the JavaScript (or any other required code) for any statistical applications that you might be using, such as Mint, Google Analytics, and so forth. I am not sure about the various WordPress statistics plugins, but they may need to be included as well. A good way to check if your stats plugin is tracking data across all themes is to either visit a few pages that you know others aren’t hitting, or else activate each of the alternate themes and check the source code of each one for the required code.

Earlier today, I realized that only several of my most recent themes included the required JavaScript for Mint and Google Analytics. I am now in the process of editing each of the 18 themes available for users at Perishable Press. Haven’t decided on whether or not both statistics apps are needed for all themes, but I will certainly be using at least one of them to keep an eye on everything.

Insane Christmas

Monday, 22 December 2008, 9:47 pm

For as long as I can remember, Christmas has always been a relatively peaceful affair. Sure there’s the usual holiday stress — traffic, shopping, presents, relatives, and all that goes with the preparation of a traditional celebration, but when it’s all said and done, you get to relax and enjoy the peace and harmony of gathering together and basking in the reason for the season: the birth of Christ.

This year, however, the stress factor has been kicked up a few notches, making for a rather insane Christmas if I do say so myself. In addition to the usual holiday chaos, we are currently purchasing a brand new home, and quickly realizing the incredible amount of work involved in the process. If you’ve ever bought a newly built home, you know exactly what I am talking about here.

Plus, as if all the paperwork, inspections, insurance, costs, and anxious anticipation weren’t enough to confound the usual holiday stress, we are also packing up everything, dealing with kids, working full-time jobs, and — beginning on Christmas Eve — moving into our new house.

It certainly is all a great joy and blessing to have such amazing things going on, but combined with the work that I do on the Web — blogging, designing, projects, helping people, and so on — it really becomes all too much rather quickly. We are doing are best to get through everything with our sanity intact, but I have to admit that this is the most insane Christmas I have ever experienced.

New (4G) Blacklist Now in Beta

Monday, 22 December 2008, 9:27 pm

Just a quick note to anyone interested in securing their websites against malicious activity, spam, and other nonsense. Several months after releasing my 3G Blacklist, I have finally begun work on the next incarnation of the blacklist: the 4G Firewall!

The first part of the blacklist is now ready for testing, and I plan on setting it up on Perishable Press within the next few days. While testing on my own site, I thought it would beneficial to also invite a few “beta” testers to run the code on their own site(s) as well.

So, if you have a site that receives its share of malicious attacks, and cracker exploits, drop me a line via the contact form at Perishable Press and I will send you the initial block of HTAccess directives. This version of the Blacklist is looking better than ever, and I look forward to releasing the complete version to the public early in 2009.

Thanks for the Free Traffic and Link Juice

Sunday, 7 December 2008, 1:26 pm

Just wanted to thank the fine folks at fafich.ufmg.br for all the free traffic and link juice. Thanks to their misapplication of my comprehensive canonicalization code, every non-canonical version of their 21,700 indexed pages points directly to my site, Perishable Press. This means that every one of their permalink URLs that is mistyped, lacks the “www” prefix, or contains the superfluous “index.php” file name is directed via permanent redirect directly to the home page of my site.

I have tried contacting the site owner(s) about this situation, but it has been over a week and I have yet to hear anything back. Hopefully, they will take notice soon and correct the issue by properly configuring their htaccess file, but in the meantime, I certainly don’t mind the extra link juice and free traffic! :)

No Plugin Needed for Feed Delay

Monday, 24 November 2008, 10:01 am

I recently saw a WordPress plugin that was designed to delay the publication of your WordPress feed by any specified time interval. While it is a good idea to carefully proofread your content before posting it, a plugin certainly is not required to do so.

As savvy WordPress users already know, WordPress has a built-in post-preview feature that enables authors to view their unpublished content as a published post. This enables authors to do any amount of proofreading and browser checking until they are satisfied with the results.

To do this, simply write your post as usual, and then click on the “Preview this post” button on the right-hand side of the screen. In older versions of WordPress (less than 2.5, I think), you actually need to save (without publishing!) the post first and then re-open it as if to continue editing. You will then see a “Preview »” link sort of hidden (due to poor CSS design) in the upper-right corner near the edit post field. Right-click on that link to open in new tab and you are good to go.

No extra plugin needed! :)

Read more on Tumblr..

Subscribe to Comments Recent Dialogue

  • Jeff Starr: Hi heywho, glad to hear you are doing well! ;) I wish I could join in the festivities.. it has been so long that I almost have forgot...
  • Rob Barrett: Thanks for posting about the Stealth Publish plugin -- just what I needed for my site. Works perfectly!...
  • Jeff Starr: Hi Chiwan, I got your email and have sent some information that may help you with this. Cheers, Jeff...
  • Chiwan: Hi. This is cool. So I can I replace the clock that comes with your Apathy theme with this clock? If that's not possible, how do ...
  • Brass Engraved: Thankyou very much for this, worked like a dream!...
  • Patrix: I'm using FeedBurner and the Feedsmith plugin for my filter blog, DesiPundit. I found your post via the WordPress page for RSS feeds ...
  • teddY: @Jessi Hance: Sorry to hear about your experience with Twitter spammers/flamers. I was once a victim of flamers and it sucks that peo...
  • heywho: Hey.... Very Nice...... I'm TOTALLY not a programmer..... but I have this thing I want to do...... so I just decided to start doi...
  • Rodrigo Nunes: NIce SEE MY BLOG http://designrn.wordpress.com/...
  • zubfatal: The Quintessential theme looks great to me, however when scrolling up or down on the page, it makes my laptop work harder than it sho...

Read more recent comments..

Attention: Do NOT follow this link!