
Loading images on your website can be a challenging task, especially if you want to ensure a fast page speed. One way to improve image loading is by using lazy loading, which loads images only when they come into view.
Lazy loading can save up to 40% of the total page weight by delaying the loading of images below the fold.
By implementing lazy loading, you can significantly improve the user experience and reduce bounce rates.
For more insights, see: B Tag Html
HTML Img Attribute
The HTML img attribute is a crucial part of web development, and it's used to handle how an image is loaded on a webpage. It accepts three string values: auto, eager, and lazy.
The lazy loading attribute is used to identify resources as non-critical and loads them only when needed, helping to optimize page loading and reduce unnecessary downloads. This technique is particularly useful for large images that are offscreen, as it defers their loading until they're expected to appear.
A different take: Is Html Still Used
Here are the supported values for the lazy loading attribute:
The lazy attribute can also help the browser focus on the most important requests and avoid unnecessary downloads, making it a useful tool for optimizing page loading.
What is the HTML Img Attribute?
The HTML Img Attribute is a crucial part of web development, and it's used to handle how an image will be loaded on a webpage.
It accepts three string values: auto, eager, and lazy. These values determine how the image is loaded, and they can significantly impact the performance of your webpage.
Lazy loading is a strategy used to identify resources as non-critical, and it defers to the loading of the webpage content until they're needed. This technique helps to optimize the page and allows it to load later.
The lazy attribute delays the loading of the image until the browser specifies that it's expected to appear shortly, which can help in optimizing the loading time of a webpage.
Here are the attribute values and their meanings:
- auto: The browser determines the lazy load of contents.
- eager: The image loads without any delay, immediately.
- lazy: The image loads when the browser specifies that it's expected to appear shortly.
What Does the HTML Img Attribute Do?
The HTML img attribute is a powerful tool that helps optimize the loading time of web pages. It allows you to control how images are loaded on a webpage.
There are three string values that the attribute accepts: auto, eager, and lazy. The lazy loading strategy is particularly useful for large images, as it defers their loading until they are needed.
Lazy loading can be useful to defer the offscreen images, allowing the webpage to load faster. It's like loading a map, you don't need to load the entire map at once, just the parts that are visible.
The lazy attribute delays the loading of an image until the browser specifies that it is expected to appear shortly. This can help optimize the loading time of a webpage by postponing the loading of images until they are needed.
Here are the supported values for the loading attribute:
The eager value loads the image immediately, regardless of its location on the page. This can be useful if you want to prioritize the loading of certain images.
Load Background Image
The loading=lazy attribute doesn't work for background images, so you'll need to use JavaScript to defer an off-screen background image.
To do this, you can use the Intersection Observer API to check whether the image is in the viewport. This API method will help you detect when the image is near the viewport and then load it.
You can also use JavaScript to modify the style attribute directly or add a class with a background-image defined in a CSS file. This will help you load the background image when it comes into view.
CSS background images are not as straightforward as the image tag. The browser needs to build the DOM tree as well as the CSSOM tree to decide if the CSS style applies to a DOM node in the current document.
Take a look at this: Dom to Html
Performance Optimization
Lazy loading is a game-changer for performance optimization. By reducing the number of images loaded upfront, you can significantly speed up page loading times.
Fewer image requests mean fewer bytes to download, resulting in faster page rendering. This is especially important for devices on slower networks or older devices.
You can use tools like Google Lighthouse audit tool to identify which images are good candidates for lazy loading and estimate the bytes you can save on initial page load. This tool even has a dedicated section for offscreen images.
Lazy loading is crucial for delivering a good user experience, as it ensures that pages become usable much earlier.
A fresh viewpoint: Html Good Practices
Image Loading Techniques
The loading=lazy attribute doesn't work for background images, so you'll need to use JavaScript to defer an off-screen background image.
You can use the Intersection Observer API to check whether the image is in the viewport, making it a great tool for lazy loading images.
The Intersection Observer API removes all the overhead of calculating if an element is in the viewport, delivering great performance out of the box.
A fresh viewpoint: Html to Pdf Api
To trigger image loads using event listeners, you need to bind events on the scroll, resize, and orientationChange events in the browser.
The scroll event is pretty clear-cut because it watches where the user is on a page as scrolling occurs.
The Intersection Observer API is relatively new, but it's a game-changer for lazy loading images.
Images load much faster using the Intersection Observer API compared to event listeners, and the action is triggered quicker as well.
Introducing a margin to the trigger point for images can provide additional time for the images to load before they enter the viewport.
You can use the root parameter along with the rootMargin parameter to increase the effective bounding box that is considered to find the intersection.
Loading images slightly in advance instead of loading exactly at the threshold can give sufficient time for the images to load completely.
Additional reading: Html on Events
Image Display and Visibility
Eager-loading images visible in the first viewport is a good idea, especially for LCP images, as it allows them to be available right away. This can be done using the browser's default eager loading.
Consider reading: Loading Coil
For images outside the initial viewport, use the loading=lazy attribute to improve performance. The browser can't lazy-load an image until it knows where the image should be on the page, which causes them to load more slowly.
The Intersection Observer API is a better way to lazy-load images, as it removes the need for math and delivers great performance out of the box. This API is relatively new, but it's already showing great results in terms of image loading times.
In fact, images load much faster using the Intersection Observer API compared to event listeners. However, this feature is not available across all browsers, so be sure to check the browser support data before using it.
Chrome made significant improvements to image lazy loading in July 2020, reducing the distance-from-viewport thresholds on fast connections and slower connections. This change makes the experience closer to JavaScript lazy loading libraries and ensures images will probably have loaded by the time a user has scrolled to them.
Here are the new distance-from-viewport thresholds in Chrome:
- Fast connections (4G): 1250px
- Slower connections (3G or lower): 2500px
Always Visible

Use the browser's default eager loading for images that are visible in the first viewport, especially for LCP images.
For images outside the initial viewport, use loading=lazy only if necessary, as it can cause them to load more slowly.
Avoid lazy loading images that are present in the viewport or at the beginning of the webpage, such as header images, marketing banners, and logos.
Images that are slightly off the viewport in the initial load, like those 500px or a single scroll from the bottom, should also be loaded upfront.
If a page is short, with less than five images outside the viewport, lazy loading may not provide any significant benefit to the end-user.
For images that are not visible in the first viewport, use loading=lazy, but be aware that the browser can't lazy-load an image until it knows where the image should be on the page.
To prevent element shift during image lazy loads, explicitly specify the size of the image's presentation using the image element's width and height attributes.
Additional reading: Html First
Distance from Viewport Thresholds
Images far below the device viewport are only fetched when the user scrolls near them. Chromium's implementation of lazy loading tries to ensure that offscreen images are loaded early enough that they finish loading by the time the user scrolls to them by fetching them well before they become visible in the viewport.
The distance threshold varies depending on the type of image resource being fetched and the effective connection type. You can find the default values for the different effective connection types in the Chromium source.
On fast connections (4G), Chrome's distance-from-viewport thresholds have been reduced from 3000px to 1250px, and on slower connections (3G or lower), the threshold has been changed from 4000px to 2500px. This change makes images load closer to the experience offered by JavaScript lazy loading libraries, while still ensuring they're loaded by the time the user scrolls to them.
You can experiment with these different thresholds by throttling the network in DevTools.
Discover more: Different Html
Improve User Experience
Lazy loading images can significantly improve user experience, especially on pages with many images. This is because the images don't load until they're actually needed, which means the initial page load is faster.
Using a dominant color placeholder, like Google and Pinterest do, can provide a more pleasing user experience. This can be achieved by scaling down the original image to a 1Ć1 pixel and then scaling it up to the size of the placeholder.
Lazy loading also reduces bandwidth consumption, which is especially beneficial for e-commerce companies that load hundreds of product images on a page. This can provide a significant improvement in initial page loads.
A low-quality, blurred version of the original image, also known as an LQIP, can be used as a placeholder to give the user some idea about what the actual image looks like and the perception that the image load is in progress. This technique has been utilized by Facebook and Medium.
For another approach, see: Placeholder Html
The Intersection Observer API can be used to detect when an element enters the viewport and take an action when it does, which makes it simple to detect when an image is visible and trigger its load. This approach provides great performance out of the box and is faster than using event listeners.
Image Loading and Performance
Lazy loading images can reduce the initial page load, but it's not always the best approach. In fact, lazy loading every image can result in a bad user experience if some images are deferred when they should not be. This is because images that are present in the viewport or at the beginning of the webpage should probably not be lazy-loaded.
The Google Lighthouse audit tool can help you identify which images are candidates for lazy loading and how many bytes you can save on the initial page load. You can also use ImageKit's website analyzer to identify if your website uses lazy loading or not.
You might enjoy: Default Value Form Html
Lazy loading might reduce the initial page load, but it also might result in a bad user experience if some images are deferred when they should not be. This is because images that are present in the viewport or at the beginning of the webpage should probably not be lazy-loaded.
A unique perspective: Html Lazy Load
Resource Prioritization
Resource Prioritization is a crucial aspect of image loading and performance. It's not just about loading images quickly, but also about ensuring that the most important resources are prioritized.
The filmstrip above shows that the background image of the hero section only loads after about 1.6s. This is because the request for the background image is only made relatively late.
The browser loads several large below-the-fold images using up the available bandwidth before loading the hero background image. This means that some of the resources will take longer to download.
Using a library to lazy load images means that you first need to load JavaScript code before starting to load the image. This can cause a delay in the display of images.
Lazy loading is critical not only to good performance but also to deliver a good user experience. By prioritizing resource loading, you can ensure that the most important images are displayed quickly.
If this caught your attention, see: Good Html Code
Avoid All Images

Avoiding lazy loading on all images isn't the best approach.
If an image is present in the viewport or at the beginning of the webpage, it's likely best not to lazy-load it. This includes header images, marketing banners, and logos.
Mobile and desktop devices have different screen sizes, so you'll want to consider the device being used when deciding what to load upfront and what to lazy load.
Any image that's even slightly off the viewport in the initial load shouldn't be lazy-loaded. This means loading images that are 500px or a single scroll from the bottom of the viewport upfront as well.
If a page is short, with less than five images outside the viewport, lazy loading might not provide any significant benefit to the end-user in terms of performance.
On a similar theme: Html Meta Viewport
Worse Lighthouse Score
I've noticed that enabling image lazy loading can sometimes lead to a worse Lighthouse Performance score.
One thing that happened when I enabled image lazy loading and later removed the loading attribute is that my Lighthouse Performance score went down.
The initial render was a lot quicker, but Time to Interactive went up because more network activity was moved to later in the page load process, keeping the network active for longer.
You can improve Time to Interactive by increasing the delay from 5s to 10s.
Image Loading and Browser Support
Native lazy loading is supported across all major browsers, like Chrome, Firefox, Safari, and Edge.
The lazy loading attribute is especially useful when loading images, particularly the LCP image. This can be seen in a waterfall diagram where a long request chain for the LCP image is visible.
The lazy loading attribute is a strategy used to identify resources as non-critical and load them only when needed. This technique helps to optimize the page and allows it to load later.
The lazy loading attribute accepts three string values: auto, eager, and lazy. Here are the key differences between them:
The lazy attribute can be used to defer the loading of offscreen images, which can be especially useful when dealing with large image sizes.
Image Loading and Offline Access
If your user loads an article and then wants to read it offline, images will be missing once they scroll down the page.
This is because images are lazy loaded, meaning they're not downloaded right away. To avoid this, you can remove the loading attribute from the img tags after the initial page load, so the images will be downloaded right away.
The trade-off here is bandwidth consumption: on a mobile plan with limited traffic, users may prefer not loading images they aren't going to see.
On a similar theme: Html Right Justify Image
Image Loading and CSS
CSS background images are not loaded by the browser until the CSS rule specifying the background image applies to an element in the current document.
This means that if the CSS rule doesn't apply to any element, the browser won't load the image. This behavior is the basis of lazy loading background images.
The browser loads the image as soon as the CSS rule applies to an element in the document.
To lazy load a CSS background image, we can trick the browser into not applying the background-image CSS property to an element until it comes into the viewport.
We do this by adding a class to the element and overriding the background-image property with a value of none in the CSS.
This is possible because an element with an ID and a class has higher specificity in CSS than an ID alone.
The browser will apply the property background-image: none to the element initially, and then change it to the actual background-image property when the image comes into the viewport.
This change triggers the load of the background image.
Suggestion: Apply Style Dynamically in Lwc Html
Image Loading and JavaScript
Image loading in HTML is a crucial aspect of web development, and JavaScript plays a significant role in it. You can trigger image load using JavaScript events, which include scroll, resize, and orientationChange events in the browser.
The scroll event is triggered when the user scrolls through a page, the resize event occurs when the browser window size changes, and the orientationChange event gets triggered when the device is rotated from landscape to portrait, or vice versa. These events can be used to recognize a change in the screen and determine the number of images that become visible on the screen.
You can use the Intersection Observer API, a relatively new feature, to detect when an element enters the viewport and take an action when it does. This API makes it simple to detect when an element enters the viewport and delivers great performance out of the box.
Here are some popular JavaScript libraries for lazy loading:
- Yet Another Lazy Loader: This library uses the Intersection Observer API and falls back to event-based lazy loading for browsers that do not yet support it.
- lazysizes: This is a very popular library with extensive functionality, including support for responsive image srcset and sizes attributes.
- jQuery Lazy: A simple library that uses a jQuery implementation.
Trigger Image with Javascript Events
The scroll event is a key player in lazy loading images with JavaScript events. It watches where the user is on a page as scrolling occurs.
To use the scroll event, we find all the images on the page that are deferred and check which ones are currently in the viewport. This is done using an imageās top offset, the current document top position, and window height.
We then pick the URL from the data-src attribute and move it to the src attribute, and the image will load as a result. This is a crucial step in lazy loading images.
Once the image has loaded, we remove the lazy class because it no longer needs to trigger an event. And, once all the images are loaded, we remove the event listeners as well.
The scroll event triggers multiple times rapidly, so for performance, we add a small timeout to our script that throttles the lazy loading function execution. This prevents the script from blocking other tasks running in the same thread in the browser.
The first three images in an example are loaded up front, with the URL present directly in the src attribute instead of the data-src attribute. This is essential for a good user experience, as these images should be made visible as soon as possible.
If this caught your attention, see: Script Element in Html
Popular JS Libraries
If you're looking to implement lazy loading in your web project, you might want to consider using a tried and tested library rather than rolling your own solution.
There are several popular JavaScript libraries that can make your life easier, including Yet Another Lazy Loader and lazysizes.
Yet Another Lazy Loader uses the Intersection Observer API and has a fallback for browsers that don't support it, which is great for most HTML elements. Unfortunately, it doesn't work on background images in CSS.
On the other hand, lazysizes is a very popular library that includes support for responsive image srcset and sizes attributes and provides superb performance.
If you're using WordPress, you might want to consider WordPress A3 Lazy Load, which comes with a robust set of features, including a fallback when JavaScript is unavailable.
Alternatively, you can use jQuery Lazy, which is a simple library that uses a jQuery implementation.
You might like: I Want to Add News to My Website Html
If you're working with Magento, you might want to consider WeltPixel Lazy Loading Enhanced or Magento Lazy Image Loader, both of which are extensions for Magento 2 and 1.x respectively.
Lastly, if you're using Shopify, you can use the Shopify Lazy Image Plugin, which is a paid plugin that enables lazy loading on your Shopify site.
Here are some popular JavaScript libraries for lazy loading:
- Yet Another Lazy Loader
- lazysizes
- WordPress A3 Lazy Load
- jQuery Lazy
- WeltPixel Lazy Loading Enhanced
- Magento Lazy Image Loader
- Shopify Lazy Image Plugin
Image Loading and Testing
Testing lazy load implementation is a must. You can do this by checking the Network > Images tab in your browser's developer tools after refreshing the page.
You should only see loaded images in the list at first. As you scroll down the page, other image load requests will get triggered and loaded.
The waterfall column in the Network > Images view shows image load timings. This can help you identify image loading issues or problems with triggering the image load.
Running a Google Chrome Lighthouse audit report on your page after implementing lazy load changes can also be helpful. Look for suggestions under the āOffscreen imagesā section for any areas of improvement.
Recommended read: Html Text Loaded
Image Loading and Miscellaneous
The Intersection Observer API is a game-changer for lazy loading images. It's relatively new, but it makes it simple to detect when an element enters the viewport and take an action when it does, eliminating the need for math and improving performance.
Using the Intersection Observer API to lazy load images can result in much faster image loading times compared to event listeners. In fact, images load much faster, and the action is triggered quicker, without making the site appear sluggish.
The support for the Intersection Observer API is not available across all browsers, however. A check on Caniuse shows that browser support varies, with some browsers supporting the feature from a certain version and up.
If you're considering using the Intersection Observer API, be aware that it has a slight impact on user experience, as the image load is triggered with a slight delay due to a timeout.
Curious to learn more? Check out: Dialog Html Support
Featured Images: pexels.com

