Preloading CSS Images

Preloading CSS Images

I can confirm that my original code seems to work. I was casually sticking to an image with a wrong path.

Here's a test :

var pic = new Image();
var pic2 = new Image();
var pic3 = new Image();

Preload images using css

I suppose that method would work, as long as the image isn't dynamically generated. The only issue with preloading using just CSS seems to be that the images download WITH the page, not after it. You can trigger the JavaScript event after the pageload is over.

Further reading:

How to preload images using CSS?

Personally I would use JavaScript in this case. But if you want only CSS you can preload all images in a div tag which is positioned in hidden part of window like follows:


<div id="preload">
<img src="" width="1140px" height="500px" alt="Home-Banner_01" />
<img src="" width="550px" height="400px" alt="PresentationBG_02" />
<img src="" width="600px" height="600px" alt="FinanceReportBG_03" />
<img src="" width="550" height="400" alt="LatestNewsBG_04" />

Preload background images

You could preload them using CSS like:

position:absolute; width:0; height:0; overflow:hidden; z-index:-1;
content:url( url( url( url(;

NOTE: You could use an array of images in the JS code and change them based on the index i.

var duration = 2500;var delay = 500;var i = 0;var images = ['', '', '', ''];
setInterval(function() { $(".myimage").css("background-image", "url(" + images[i] + ")"); i++;}, duration + delay)
.myimage {  height: 500px;  width: 500px;  transition: background-image 1s linear;  background-image: url('');  background-size: cover;}
body::after { position: absolute; width: 0; height: 0; overflow: hidden; z-index: -1; content: url( url( url( url(;}
<script src=""></script><div class="myimage"></div>

preload hidden CSS images

When you said other ways do you mean ones that don't use Javascript?

<script language="JavaScript">
function preloader()
// counter
var i = 0;

// create object
imageObj = new Image();

// set image list
images = new Array();

// start preloading
for(i=0; i<=3; i++)

Other none JS ways are to place some html in your page somewhere so it's not seen:

<image src="picture.jpg" width="1" height="1" border="0">

or HTML...

<img src="images/arrow-down.png" class="hiddenPic" />

...and CSS...

.hiddenPic {

More JavaScript Methods:

function preload(images) {
if (document.images) {
var i = 0;
var imageArray = new Array();
imageArray = images.split(',');
var imageObj = new Image();
for(i=0; i<=imageArray.length-1; i++) {
//document.write('<img src="' + imageArray[i] + '" />');// Write to page (uncomment to check images)

Then load the images using something like:

<script type="text/javascript">

Related Topics

Leave a reply