Lazy loading CSS background images for better website loading performance
Find out how to lazy load CSS background images to improve your website loading performance.
Every single HTTP request decreases loading performance. For a simple image, the attribute loading="lazy"
can be used in order to defer the loading of off-screen images until the image appears on the screen. Lazy loading of images improves performance by reducing initial page load time, initial page weight, initial page rendering, and system resource usage.
- Initial page load time – with lazy loading, the page is loaded faster because only images that are visible on the screen are loaded.
- Initial page weight – because images that aren’t visible won’t be downloaded. Hence, the whole page weight will be smaller.
- Initial page rendering – less images to render, the faster the whole page will be rendered for the user.
- System resource usage – this refers to less resource usage like CPU, network, etc.
Can you lazy load background images?
As of now, browser-level background images can’t be lazy loaded, but with some small JavaScript support using Intersection Observer API, it is possible.
Set the goals for lazy loading CSS background
We want to support single and multiple images (through the image-set
attribute) and load the background image in two possible ways. Let’s explore an example for a single image:
Unfortunately, the background image will be loaded regardless of being on-screen or off-screen. With exception when element <div>
has defined display: none;
.
What we need is to stop loading the background image until it’s visible on the screen. Here we can use Intersection Observer API. What is it?
The Intersection Observer API provides a way to asynchronously observe changes in the intersection of a target element with an ancestor element or with a top-level document’s viewport.
The solution
The solution should be fully automated. We want to just set the attribute data-background-image="URL to the image here"
on the HTML element or directly in the CSS.
Let’s then set up our goals:
Set the background through
data-background-image="URL to the image here"
or directly in the CSS.- Initialize automatically lazy loading background when the page is ready based on
DOMContentLoaded
event. At this point, the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, or subframes to finish loading. - When the Intersection Observer API is not supported then simply load all background images. Note that polyfill for Intersection Observer is available for the browser that doesn’t support it, but for our simplification here we’ll skip this part and just allow the browser to load all background images.
Workable example
Here is a workable example of lazy loading CSS background images
The previous example is available on Sitelint Codepen Developer Workspace.
Download package
The package @sitelintcode/css-lazy-loading-background is available in the npm repository.
Discussions
- Support lazy-loading CSS background-images – a discussion on WHATWG about implementing natively lazy loading backgrounds.
This can be further improved by avoiding loading images, videos, and audio that are less than 1 second long on the user’s screen. For example, during fast scrolling. Read more about improved lazy loading for images, videos, and audio.
How to set lazy loading images on other libraries
- SplideJS – to enable lazy loading images for SplideJS replace
<img src="[path to the image]" alt="">
with<img data-splide-lazy="path to the image" alt="">
.
Comments