CSS Image Preloader
In today’s age of high-speed internet, the last thing that users want to do is to wait for a website to load even if it is for a few seconds. One of the “musts” for today’s web design is to create a website that loads quickly enough so users don’t leave your site before the whole page is displayed.
To minimize this problem, many sites are designed to preload images in order to speed up page display. JavaScript is the most common way to preload images but it is not the only option available. An alternative to JavaScript is to use the CSS display: none; property. This technique may be more reliable and it requires less complex code. Easy!
Preloading images using JavaScript is a good technique, however, browsers must be JavaScript-enabled and have JavaScript turned on. Without JavaScript, the images won’t preload.
CSS: Our Best Preloading Friend!
CSS offers us a simple and reliable option. By using display: none;, we tell the browser to request an image from the server, but not to display it on the page. The browser just requests the image from the server and caches it for future use.
Common Uses:
- Use in the home page to load images that will be used on the rest of the website
- Cache all the hover images in your site for menus, slide shows, etc.
The CSS Code:
This is a very simple technique. All we need to do is to place the img tags at the beginning, right under the opening body tag and insert CSS style directly within the img tag:
...
</head>
<body>
<!--preloaders-->
<img src="image_1.jpg" style="display:none;" alt=""/>
<img src="image_2.jpg" style="display:none;" alt=""/>
<img src="image_3.jpg" style="display:none;" alt=""/>
<!--end preloaders-->
...
- Very Important: The empty alt tag is important because screen readers will simply ignore the image. You MUST insert the empty alt tag otherwise your website will fail the W3C’s accessibility standards!
- A good idea is to insert comments like in the example above just to separate and locate your rollover images
Summary:
That’s it for this technique! Very simple, almost no code and same results as JavaScript.
1 Comment