Welcome to the new design! Please report any bugs or issues, thanks :)
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!!

Jeff Starr
About the Author
Jeff Starr = Web Developer. Book Author. Secretly Important.
The Tao of WordPress: Master the art of WordPress.

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. Jeff Starr

    @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 »
GA Pro: Add Google Analytics to WordPress like a pro.
Thoughts
Dashlane recently redesigned, stating proudly that they "removed all filigree". They should have kept it, as the app now looks generic and boring.
Working on integration for setaPDF + EDD on the new books subdomain. Good times.
Toggle visibility of hidden files on Mac: Cmd + Shift + .
Great tool for checking browser caching for web pages and all included files.
The new minimalist design styled by 14KB of CSS (uncompressed and un-minified). That covers 850+ posts and pages spanning 15 years of diverse content.
Amazing (and sad) to think that, in the year 2020, Internet Explorer still doesn't support the placeholder attribute, which is supported by all other major browsers.
Finishing up with the latest redesign for Perishable Press! Should be live this week or early next.