Spring Sale! Save 30% on all books w/ code: PLANET24
Web Dev + WordPress + Security

CSS Throwdown: Preload Images without JavaScript

[ Preload Images with CSS ] Clean, easy, effective. You don’t need no stinking JavaScript to preload your images. Nope. Try some tasty CSS and (X)HTML instead! Here’s how to do it with only two easy steps..

Step 1 — Place this in your CSS file:

div#preloaded-images {
   position: absolute;
   overflow: hidden;
   left: -9999px; 
   top: -9999px;
   height: 1px;
   width: 1px;
}

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

<div id="preloaded-images">
   <img src="https://perishablepress.com/image-01.png" width="1" height="1" alt="" />
   <img src="https://perishablepress.com/image-02.png" width="1" height="1" alt="" />
   <img src="https://perishablepress.com/image-03.png" width="1" height="1" alt="" />
</div>

..and that’s a wrap! All images are preloaded and ready for calling as you please. Completely valid, standards-compliant image preloading via CSS and (X)HTML!!

About the Author
Jeff Starr = Web Developer. Security Specialist. WordPress Buff.
Wizard’s SQL for WordPress: Over 300+ recipes! Check the Demo »

68 responses to “CSS Throwdown: Preload Images without JavaScript”

  1. GAH i made a div tag in my last comment! lol i meant

    Create a Div tag at the bottom of your page after your footer

    Like so: “”

  2. @Caderial: I think we get what you’re saying, but you know that method is not going to work. The way your code is written, only the last instance of the background-image property is going to be rendered by the browser. That’s just the way CSS works — in cascading fashion.

    As I explain here, there is a way to do what you’re thinking with CSS3.

    For tons more on preloading images, check the list of “Related Posts” at the end of the article.

  3. @David Bowman – What contribution did your “wise” words bring to this post? In a related topic you authoritatively attempt to suggest that “triad” (underground society) is a better word than “triage” (which is also an uninspired choice in the context, but that’s an entirely different matter). I’m sorry, but you lose any sort of remaining credibility and pretty much anything you say is mere uninformed opinion. Go and educate yourself before you start another tirade.

  4. Increazon 2010/10/13 7:23 am

    i will use it now for my buttons on the web site http://apokalipsis-2012.ru/ because it blinking now. check it later if the code really work

  5. its looks like it is not working. at least when i mouse over butoon on site, it connecting to server (i can see it using FIREBUG FOR FIREFOX). tHE PATH IS right. so i do not know. hope it will work because i not going to give time to this goal any more. Peace

  6. Jeff,

    When a visitor is that rude (yes, I’m talking about David Bowman) there is no need for you to remain polite. You will not lose or offend visitors.

    Tell him to buzz off, and to better himself with something more worthy of his stature. Speak your mind and tell the prick to beat it.

  7. Hello

    I would like to know how to implement the preload css in my site… I have pasted the css code on the main.css… Now at album_view.htm, what should I do? I have hundreds of images all at a folder in my site… How do I call this folder to preload…

    You can view our present album_viewer here…

    http://regidordigital.com/view-album/3531/confecciones-placi

    Thx very much
    Alberto

  8. Hi Jeff,
    Congratulations on solid article write ups. Very good information available here.

    Also wanted to ask you if you have considered installing a WordPress Plugin for pagination. Currently the comments span over a huge vertical length. Although you have the scroll to top button installed, IMO, pagination will help a lot. Just my 2 cents to make your blog even better…

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
WP Themes In Depth: Build and sell awesome WordPress themes.
Thoughts
I live right next door to the absolute loudest car in town. And the owner loves to drive it.
8G Firewall now out of beta testing, ready for use on production sites.
It's all about that ad revenue baby.
Note to self: encrypting 500 GB of data on my iMac takes around 8 hours.
Getting back into things after a bit of a break. Currently 7° F outside. Chillz.
2024 is going to make 2020 look like a vacation. Prepare accordingly.
First snow of the year :)
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.