Preloading images using CSS only
In the below code I am randomly choosing the body
element, since it is one of the only elements guaranteed to exist on the page.
For the “trick” to work, we shall use the content
property which comfortably allows setting multiple URLs to be loaded, but as shown, the ::after
pseudo element is kept hidden so the images won’t be rendered:
body::after{
position:absolute; width:0; height:0; overflow:hidden; z-index:-1; // hide images
content:url(img1.png) url(img2.png) url(img3.gif) url(img4.jpg); // load images
}
Demo
it’s better to use a sprite image to reduce http requests…(if there are many relatively small sized images) and make sure the images are hosted where HTTP2 is used.