CSS only technique for showing a loader for slow loading images

Recently I had a requirement to show images inside an overlay, the problem was that the image that had to loaded was coming loading very slowing and the user couldnt get any sort of indication that the image was being loaded.

Ofcourse one clear solution was to call it via HTTPRequestObject but obviously that would have been an overkill for loading just an image, I came up with this solution (and i dont make any claims that no one has ever done this before) to use css only to method to show a loading image which is later replaced once the actual image has downloaded.

NOTE: This technique doesn’t work for transparent images.

The markup:

<div id="app-mod-mod1" class="mod">
<img src="slow_loading.gif">
</div>

CSS:

#app-mod-mod1 {
background: #FF url('ajaxyloader.gif') no-repeat fixed top left;
}

This will cause the image ajaxloader.gif to be loaded as the background for the div and it will get hidden behind the non transparent slow loading image.

A simple solution but works, without having to rely on javascript to updated the css classname and other cross browser incompatibility issues.

About rp

Architect for large, highly scalable LAMP applications and Technical Manager with special focus on metrics based continuous improvement of teams and products. Rajat has close to a decade of experience of a very wide range of skills related to infrastructure, middleware, app servers all the way to front-end technologies and software development methodologies including agile, iterative waterfall, waterfall as well as ah-hoc startup using the right approach in the right context to reduce time to market.