How to Defer Offscreen Images? – Guide

defer offscreen images

Latest update: August 28, 2024

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.

What is an Offscreen Deferred Image?

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.

How to Defer Offscreen Images?

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.

Pros and Cons of Defer Offscreen Images

Pros

  • Improved Page Load Time: By loading only the necessary images, the page loads faster initially;
  • Bandwidth Savings: Images are only loaded as needed, which can save bandwidth for both the server and the user, especially beneficial for users on limited data plans.

Cons

  • Potential for Layout Shift: If image dimensions are not specified, deferring images can cause layout shifts as images load, affecting the user experience;
  • Dependency on JavaScript: When deferring images using JavaScript, users with disabled JavaScript may not see the images.

What are Common Problems with Deferred Offscreen Images?

Several challenges can arise when implementing deferred loading for offscreen images:

  • Images Not Loading: If the deferring script fails or if JavaScript is disabled in the user’s browser, images may not load;
  • Layout Shifts: Without proper handling, such as specifying explicit dimensions for images, deferred loading can cause unexpected layout shifts;
  • SEO Considerations: Ensuring search engines can crawl and index deferred images requires careful planning.

The Best Practice in Deferring Offscreen Images

To effectively defer offscreen images while minimizing potential drawbacks, follow these best practices:

  • Use Native Lazy Loading: Whenever possible, use the loading=”lazy” attribute for images. This native approach is supported by most modern browsers and requires no additional JavaScript;
  • Specify Image Dimensions: To prevent layout shifts, always include width and height attributes for images or ensure container dimensions are defined;
  • Test Across Browsers: Ensure your deferring method works consistently across different browsers and devices;
  • Consider SEO: Make sure search engines can still crawl and index images appropriately. Using <noscript> tags with a standard <img> tag inside can provide a fallback for search engines and users without JavaScript.

By adhering to these guidelines, you can optimize your website’s performance without sacrificing user experience or search engine visibility.

Guidelines for Deferring Offscreen Images

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:

HTML Markup

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">

JavaScript Implementation

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>

Conclusion

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.