Yogesh Chauhan's Blog

How to progressively load images and add a blurry placeholder?

in CSS & JavaScript on August 2, 2021

Loading images progressively is very important especially when you have tons of images on your web page.

MDN has a really nice guide on Progressive loading.

Example 1: With CSS blur

Here’s a demo I created using MDN code:

Example 2: Without CSS blur

I created another example that doesn’t actually require you to add CSS blur property.

Here’s what I did.

I load the image with smaller height and width first since your browser will just start downloading the image when it sees the src.


<img loading="lazy" 
  src="https://picsum.photos/id/1016/10/10" 
  data-img="https://picsum.photos/id/1016/200/200" 
  width="200" 
  height="200" 
  class="image" 
  alt="Mountain" 
/>


As you can see, I also have another actual larger image in data-img attribute. That actual image will be fetched using JavaScript.

The idea is to change the src for the images and let browser fetch the images.


Here’s the working example:


Most Read

#1 Solution to the error “Visual Studio Code can’t be opened because Apple cannot check it for malicious software” #2 How to add Read More Read Less Button using JavaScript? #3 How to check if radio button is checked or not using JavaScript? #4 Solution to “TypeError: ‘x’ is not iterable” in Angular 9 #5 How to uninstall Cocoapods from the Mac OS? #6 PHP Login System using PDO Part 1: Create User Registration Page

Recently Posted

#Apr 8 JSON.stringify() in JavaScript #Apr 7 Middleware in NextJS #Jan 17 4 advanced ways to search Colleague #Jan 16 Colleague UI Basics: The Search Area #Jan 16 Colleague UI Basics: The Context Area #Jan 16 Colleague UI Basics: Accessing the user interface
You might also like these
How to define constants in PHP?PHPColumn and Table Alias in PostgresPostgresHow to select multiple values in React dropdown?ReactCreate a responsive pricing table using simple HTML and CSSCSSHow to Block IPs and User Agents using code in Drupal or WordPress?DrupalSanitize inputs using built-in WordPress functionsWordPressWhat is PostgreSQL? How similar or different it is from SQL?PostgresHow to remove a specific item from an array in JavaScript?JavaScriptHow to Recognize an Array in JavaScript?JavaScriptHow to modify AutoSave Interval using code in WordPress?WordPressWhat are components in Angular?AngularHow does while loop work in SCSS?SCSS