Home
/
Website Help
/
Speed And Uptime
/
Lazy Loading Images - What Is It & How to Do It? 

Lazy Loading Images - What Is It & How to Do It? 

Images are a key part of any website. However, they can also significantly impact page loading speed, especially on image-heavy applications. This is important since faster websites provide a better user experience and rank higher in search engines.

One way to optimize image loading is by using lazy loading—a technique that improves performance without compromising visual quality.

In this article, you’ll learn what lazy loading is, how it works, and how to implement it on your website. We’ll also explore lazy loading best practices and common mistakes to avoid.

Lazy loading is an optimization technique that defers the loading of images and other media content until they are needed.

Instead of loading all images as soon as the page opens, the browser loads only the images visible in the user’s screen. Images located further down the page are loaded dynamically as the user scrolls.

This method improves page speed, reduces the amount of data transferred, and minimizes the strain on both the user’s device and the server.

By needing fewer resources upfront, lazy loading provides a smoother browsing experience—especially on mobile devices or when one has a slower network connection.

Lazy loading is commonly used for images, but it can also apply to videos, iframes, and other multimedia content.

How Lazy Loading Works

Lazy loading works by delaying the browser’s request for certain resources until they are about to be displayed on the screen.

When a user opens a webpage, only visible images are loaded immediately. Other image load requests are only sent if the user scrolls down.

The process typically involves these steps:

  1. Initial Page Load: The browser loads the essential page content and visible images.
  2. Placeholder Images: Non-visible images are replaced with lightweight placeholders or empty image tags.
  3. User Interaction: As the user scrolls or interacts with the page, the browser detects when an image is about to come into view.
  4. Image Loading: The actual image file is loaded from the server only when the image is close to or within the visible viewport.

Lazy loading can be implemented using HTML attributes, JavaScript, or plugins—all of which we’ll explore in the next sections.

How to Implement Lazy Loading

There are several ways to implement lazy loading on a website, depending on the site’s setup and your technical skills.

The most common methods include using native HTML attributes, JavaScript solutions, and built-in features in CMS platforms such as WordPress.

1. Lazy Loading in WordPress

WordPress makes implementing lazy loading easy, even without coding knowledge.

1.1. Automatic Lazy Loading

By default, WordPress automatically adds the loading=”lazy” attribute to images inserted through the block editor or using the wp_get_attachment_image() function.

This automatic feature improves performance without requiring additional configuration.

1.2. Via Plugins

For more advanced lazy loading options, you can use WordPress plugins. Popular plugins offer additional features like loading animations, CSS exclusions, and lazy loading for background images.

The SiteGround Speed Optimizer plugin not only enables lazy loading but also optimizes images, caches files, and improves overall site performance.

It’s a powerful, all-in-one solution to make your WordPress site load faster on both mobile and desktop devices.

A promotional banner for the SiteGround Speed Optimizer plugin, with a bullet list describing its popularity, and a Get Now button.


To enable lazy loading with the SiteGround Speed Optimizer plugin:

1. Go to your WordPress dashboard.

2. Navigate to Speed Optimizer > Media.

Screenshot of the Speed Optimizer Media Optimization settings in WordPress, with a highlight on the option to switch on Lazy Load Media.

3. Enable the Lazy Load Media option.

A table with two columns listing the pros and cons of using plugins for lazy loading. Pros are easy setup, no coding needed and advanced customization options. Cons are that features may add extra overhead.

2. Using Native HTML Attributes

Modern browsers support native lazy loading through the loading=”lazy” attribute in HTML.

This method is the simplest way to add lazy loading without writing additional code.

Example:

<img src="image.jpg" alt="Example Image" loading="lazy">

The loading=”lazy” attribute tells the browser to defer loading the image until it’s about to enter the viewport.

A table with two columns listing the pros and cons of using native HTML attributes for lazy loading. Pros are that it is easy to implement, no external scripts are required and it's supported by most modern browsers. Cons are limited older browser support and customization.

3. Using JavaScript

JavaScript provides more flexibility and control over lazy loading, especially for older browsers or more advanced use cases.

Two common methods are LazyLoad.js and the Intersection Observer API.

3.1. LazyLoad.js

LazyLoad.js is a lightweight JavaScript library that simplifies lazy loading for images and other media elements. It triggers the lazy loading function execution only when images enter the viewport.

To use it:

1. Include the library in your project:

<script src="https://cdnjs.cloudflare.com/ajax/libs/vanilla-lazyload/17.8.3/lazyload.min.js"></script>

2. Replace the src attribute in your image tags with data-src:

<img data-src="image.jpg" alt="Example Image" class="lazy">

3. Initialize LazyLoad.js with JavaScript:

var lazyLoadInstance = new LazyLoad({

elements_selector: ".lazy"

});
A table with two columns listing the pros and cons of using lazyload.js. Pros are easy setup, works in most browsers and customizable options. Cons are the slight performance overhead and the external library requirement.

3.2. Intersection Observer API

The Intersection Observer API is a modern JavaScript feature that detects when elements enter or leave the viewport.

Example:

<img data-src="image.jpg" alt="Example Image" class="lazy">


const lazyImages = document.querySelectorAll(".lazy");

const observer = new IntersectionObserver((entries) => {

  entries.forEach((entry) => {

    if (entry.isIntersecting) {

      const img = entry.target;

      img.src = img.dataset.src;

      observer.unobserve(img);

    }

  });

});


lazyImages.forEach((img) => observer.observe(img));
A table with two columns listing the pros and cons of using the intersection observer API for lazy loading. Pros are that it's built into modern browsers, doesn't require external libraries and offers better performance. Cons are lack of support in old browsers and more coding required.

Best Practices for Lazy Loading Images

Implementing lazy loading can improve your website’s performance, but following best practices ensures the technique works effectively without compromising user experience or SEO.

1. Prioritize Above-the-Fold Images

Always exclude above-the-fold images from lazy loading.

These are the images visible without scrolling and should load immediately to avoid layout shifts and improve perceived performance.

In HTML, simply omit the loading=”lazy” attribute for these images.

2. Use Placeholder Images

Displaying a placeholder while the image loads improves the visual experience and prevents empty spaces. You can use a low-quality image or a solid background color as a placeholder.

Example with CSS:

.lazy {

background-color: #f3f3f3; /* Placeholder color */

min-height: 200px; /* Image height */

}

3. Optimize Image Sizes and Formats

Lazy loading works best with optimized images. Use modern formats like WebP and compress images before uploading them.

Tools like the SiteGround Speed Optimizer plugin can automatically convert images to WebP and optimize their size.

Screenshot of the Speed Optimizer Media Optimization settings in WordPress, with a highlight on the option to Use WebP images.

4. Test Performance and Compatibility

Always test lazy loading on different devices and browsers for compatibility.

Tools like Google PageSpeed Insights or Lighthouse can help measure performance improvements.

5. Combine Lazy Loading with Other Optimization Techniques

Lazy loading works best alongside other performance optimizations like:

SiteGround users can easily apply these optimizations using the Speed Optimizer plugin and the integrated features in their Site Tools dashboard.

Common Lazy-Loading Mistakes & How to Avoid Them

While lazy loading improves performance, improper implementation can lead to issues that negatively impact user experience and SEO.

Here are some common mistakes and how to fix them.

1. Lazy Loading Above-the-Fold Images

Lazy loading images at the top of the web page can cause delayed content rendering and layout shifts. This makes your site feel slow.

Fix: Exclude above-the-fold images from lazy loading by removing the loading=”lazy” attribute from key images, such as logos, your header image, and featured thumbnails.

2. Not Providing Image Dimensions

If image dimensions aren’t set, the browser doesn’t know how much space to reserve, leading to content shifts as images load.

Fix: Always define the width and height attributes for images to prevent layout shifts.

<img src="image.jpg" alt="Example Image" width="600" height="400" loading="lazy">

3. Lazy Loading Critical Images in Sliders or Backgrounds

Images inside sliders or set as CSS background images may not trigger lazy loading properly, causing delays in content display.

Fix: Preload critical slider images and use JavaScript-based lazy loading to control when the actual background image property is applied.

Example of using the background image CSS property:

.lazy-bg {

background-image: url("placeholder.jpg");

}

.lazy-bg.loaded {

background-image: url("real-image.jpg");

}

JavaScript example:

document.querySelectorAll('.lazy-bg').forEach(el => {

el.classList.add('loaded');

});

4. Overusing JavaScript for Lazy Loading

While JavaScript-based lazy loading solutions offer flexibility, overusing them increases resource usage and may not work on all browsers.

Fix: Use native HTML lazy loading (loading=”lazy”) whenever possible. Use JavaScript-based solutions only when more control is needed.

5. Forgetting SEO and Accessibility Considerations

Lazy-loaded images may not be indexed properly by search engines if they lack proper attributes or JavaScript execution. Additionally, missing alt attributes can harm accessibility.

Fix:

  • Make sure images are discoverable by search engines by using proper img elements instead of background images for important content.
  • Always include meaningful alt attributes for screen readers.
<img src="product.jpg" alt="Red running shoes" loading="lazy">

Following these best practices will help you avoid common lazy-loading issues while ensuring a seamless experience for users and search engines.

Key Takeaways on Lazy Loading Images

Lazy loading is a powerful technique that improves website performance by delaying the loading of images until they are needed. It reduces initial page load times, saves bandwidth, and enhances user experience—especially on mobile devices and slower connections.

In this guide, we covered:

  • What lazy loading is and how it works
  • Different implementation methods, including HTML attributes, JavaScript, and WordPress plugins
  • Best practices for optimal performance
  • Common mistakes and how to avoid them

For WordPress users, the SiteGround Speed Optimizer plugin offers an easy way to enable lazy loading alongside other performance optimizations.

By following the tips outlined in this article, you can significantly speed up your site without compromising content quality.

A promotional banner for SiteGround's Managed Hosting plans, listing key features such as premium caching technology, top-notch security solutions, and expert support.

Share This Article