Website Building » Shopify » How Do I Defer Offscreen Images in Shopify?

How Do I Defer Offscreen Images in Shopify?

Last updated on December 22, 2022 @ 7:54 am

Lazy loading is a technique that delays loading of images until they’re needed. This can make your site load faster, especially if you have many images.

Shopify uses a third-party app called Smush to automatically lazy load images on your store. Smush lazy loads images by inserting a placeholder in the src attribute of img tags.

When the image is about to enter the viewport (visible part of the screen), the src attribute is updated with the actual image URL.

You can also lazy load background images in Shopify. To do this, add the data-src attribute to the element with the background image.

The value of data-src should be the URL of the image. When the element is about to enter the viewport, Shopify will update the element’s style with the actual background image URL.

PRO TIP: If you are using Shopify to defer offscreen images, be aware that there is a potential for your images to not load properly. This can happen if the image files are too large or if the internet connection is too slow. If you are having problems with your images not loading, try to reduce the file size or increase the speed of your internet connection.

Here’s an example of how to lazy load a background image:

<div class="lazy-load" data-src="https://example.com/image.jpg"></div>

When the div is about to enter the viewport, Shopify will update the element’s style with the following:

<div class="lazy-load" 
data-src="https://example.jpg" 
style="background-image: url('https://example.jpg');">
</div>
Kathy McFarland

Kathy McFarland

Devops woman in trade, tech explorer and problem navigator.