Ihor Masalov
Latest update: May 20, 2025
In pursuing optimal website performance, deferring images represents a pivotal strategy, particularly for content-rich pages. This guide elucidates a straightforward approach to postponing the loading of images without relying on jQuery or lazy loading techniques, directly addressing one of the primary culprits behind sluggish page rendering: images loading prematurely.
An offscreen deferred image refers to an image on a webpage that is not loaded immediately when the page is accessed. Instead, its loading is postponed until it is likely to be needed—this can mean waiting until other, more critical page elements have loaded or until the user scrolls the image into view. This technique optimizes resource loading, improving page speed and user experience by reducing initial load times.
Deferring offscreen images can be achieved through several methods, including using JavaScript or taking advantage of newer HTML attributes. A common JavaScript method involves substituting the src attribute of an <img> tag with a placeholder and using a data-src attribute for the actual image URL. The image is only loaded when certain conditions are met, such as the image entering the viewport. Modern browsers also support the loading=”lazy” attribute for <img> tags, which simplifies the process without needing additional JavaScript.
Several challenges can arise when implementing deferred loading for offscreen images:
To effectively defer offscreen images while minimizing potential drawbacks, follow these best practices:
By adhering to these guidelines, you can optimize your website’s performance without sacrificing user experience or search engine visibility.
The essence of deferring images lies in altering the default behavior of browsers that eagerly download all images upon accessing a page. By substituting the image source (src) with a placeholder and then using JavaScript to swap the placeholder with the actual image source as needed, we can control when the image is loaded:
Use a base64 encoded 1×1 pixel as a placeholder in the src attribute, and place the actual image URL in a data-src attribute.
<img src="data:image/png;base64,R0lGODlhAQABAAD/ACwAAAAAAQABAAACADs=" data-src="path/to/your-image.jpg">
Execute a simple script that replaces the placeholder with the actual image URL after the page has loaded or another condition is met.
<script>function init() { var imgDefer = document.getElementsByTagName('img'); for (var i=0; i<imgDefer.length; i++) { if(imgDefer[i].getAttribute('data-src')) { imgDefer[i].setAttribute('src', imgDefer[i].getAttribute('data-src')); } }}window.onload = init;</script>
Deferring offscreen images emerges as a robust strategy in the web performance toolkit, capable of markedly improving page load times and user experience. By understanding and implementing the recommended techniques, webmasters can navigate the balance between rich media content and optimal website performance, ensuring their site remains fast and responsive across all devices.