A simple web component to progressively enhance image placeholders once they are in the viewport.
Example
savedata attribute to load images on demand.30x201020x680 (@2x)Features
According to Web Almanac, images account for a large portion of the page weight of a typical website and can have a significant impact on your site's LCP performance.
The <progressive-image> element extends the HTML <img> element with features to improve the initial page weight:
- Faster page load: Images are loaded only as they enter the viewport, using native browser lazy loading with placeholders
- Visual stability: Prevent layout shifts when loading images
- Save data option: Load images only on demand
- No dependencies: Framework agnostic web component
Install
Install via npm
Usage
1. Include the script & stylesheet within your application
or
2. Use the <progressive-image> element markup
Use a solid color, LQIP, or SQIP image placeholder. This will hint at the content of the progressive image before it loads.
Attributes
srcSpecifies the image to displaysrcsetOne or more image candidate stringssizesComma-separated list of source size descriptorssavedataBoolean attribute to load the images only after a click/tap on the placeholder image.
By default enabled for slow connections (slow-2g|2g|3g).
Styling states
A CSS class loadable is present on the <progressive-image> element when the image is ready to load on user interaction (click).
Used for slow connections or when the savedata attribute is present.
A CSS class loading is present on the <progressive-image> element while the image is loading.
A CSS class loaded is present on the <img> children of the <progressive-image> element when the image was loaded.
Examples
For more examples checkout the CodePen <progressive-image> element collection.