Skip to main content

How to deal with images loading on a website?

Lovekesh Kumar
Written by Lovekesh Kumar
Article on 4 min read

Get started with DigitalOcean and host your projects for free. Get $100 credits now!

Images are something that explains a lot about your brand or your story. Using images on your website can help a user to understand things more easily and efficiently.

Now the first question that may come up to your mind is should I use images on my website to add more visual descriptions of my brand and products?

Yes! You need them on your page and they do more than just give your audience a chance to see what you have to offer. They are critical when it comes to generating more qualifies leads.

But you can't achieve your goal just by adding some high-resolution images to your website. This may end up affecting your website loading speed and performance.

As page speed is a primary factor of your website's SEO ranking, so you can't just compromise with your site's speed and performance.

According to Google most of the websites don't load fast enough which means they are losing tonnes of conversions. Just make sure that you are not one of them!

So what are the efficient solutions which can help in making your website super fast even with a lot of images on the website?

Let's find them one by one 😀

Avoid unnecessary decorative images

Always make good research about the image that you are placing on your web page. Evaluate how it is gonna help a visitor to visualize the scene.
Using more and more images for no cause will degrade the webpage in two ways:

  • It will make more HTTP requests
  • It will load image resources and hence webpage will take time to load.

Do not use images everywhere

Let's take an example: You are using small images which are working as icons on your webpage. Each Image will be served after an individual HTTP request and the webpage will take more time to load completely.

So the better option is that try to avoid using images as icons. There is a better option for that called SVGs which can be included in your HTML and doesn't require to make any request to load themselves.

Serve images with the right file format

There are different image file formats, for example, PNG, JPEG, and GIF. There are some other next-gen formats also like JPEG 2000, JPEG XR, and WebP that have superior compression and quality characteristics compared to their older JPEG and PNG counterparts.

Encoding your images in these formats rather than JPEG or PNG means that they will load faster and consume less cellular data.

WebP is supported in Chrome and Opera and provides better lossy and lossless compression for images on the web.

Compress Images

Bulky images can increase the downloading time of the browser and it will result in affecting your site speed score and hence website speed index and first contentful paint will get affected.

To overcome this situation you might like the option of compressing the images. Several online tools can help you in reducing your image size and prevent your image quality at the same time. Reduced file size can improve the load time of the website.

Some online image compression websites you may like

Lazy-loading images

Lazy Loading defers the loading of an image that is not needed on the page immediately. When a user will scroll down then the image will get loaded instantly.

Using browser-level lazy-loading

Chrome and Firefox both support lazy-loading with the loading attribute. This attribute can be added to <img> elements, and also to <iframe> elements. A value of lazy tells the browser to load the image immediately if it is in the viewport, and to fetch other images when the user scrolls near them.

<img src="/cat.jpg" alt="Image" loading="lazy" />

Lazy-loading libraries

The following libraries can be used to lazy-load images.

  • Lazysizes - fast (jank-free), SEO-friendly and self-initializing lazyloader for images (including responsive images picture/srcset), iframes, scripts/widgets and much more. It also prioritizes resources by differentiating between crucial in view and near view elements to make perceived performance even faster.
Lazysizes does not need any JS configuration: Add the class"lazyload" to your images/iframes in conjunction with a data-src and/or data-srcset attribute. Optionally you can also add a src attribute with a low quality image.
<!--Include script in your webpage-->
<script src="lazysizes.min.js" async=""></script>

<!-- non-responsive: -->
<img data-src="image.jpg" class="lazyload" />

<!-- responsive example with automatic sizes calculation: -->
<img
    data-sizes="auto"
    data-src="image2.jpg"
    data-srcset="image1.jpg 300w,
    image2.jpg 600w,
    image3.jpg 900w" class="lazyload" />

<!-- iframe example -->
<iframe frameborder="0"
	class="lazyload"
    allowfullscreen=""
    data-src="//www.youtube.com/embed/ZfV-aYdU4uE">
</iframe>
  • Vanilla-lazyload - lightweight option for lazy-loading images, background images, videos, iframes, and scripts. It leverages Intersection Observer, supports responsive images, and enables browser-level lazy loading.
<!--Lazy Image-->
<img alt="A lazy image" class="lazy" data-src="lazy.jpg" />

<!--Lazy image with low quality placeholder:-->
<img alt="A lazy image" class="lazy" src="lazy-lowQuality.jpg" data-src="lazy.jpg" />

<!--Lazy responsive image with srcset and sizes:-->
<img
  alt="A lazy image"
  class="lazy"
  data-src="lazy.jpg"
  data-srcset="lazy_400.jpg 400w, 
    lazy_800.jpg 800w"
  data-sizes="100w"
/>

Similarly you can play around with these given lazy-load libraries.

  • lozad.js is a another lightweight option that uses Intersection Observer only. As such, it's highly performant, but will need to be polyfilled before you can use it on older browsers.
  • yall.js is a library that uses Intersection Observer and falls back to event handlers. It's compatible with IE11 and major browsers.
  • If you need a React-specific lazy-loading library, consider react-lazyload. While it doesn't use Intersection Observer, it does provide a familiar method of lazy loading images for those accustomed to developing applications with React.

Conclusion

In this tutorial, we have discussed different aspects of using images to visualize your web pages without affecting the website's performance and loading time.

If you are in trouble with your website's loading speed, performance, and SEO then you can hire a professional that has built this great website with a loading speed of 0.7 seconds! - Contact Expert.

Something Missing?

If something is missing in this post or if you found some part confusing, then you can:

We love hearing from you!

Give Feedback

Thank you for the feedback! (Join Codebulbs Writers Club)

Sorry to hear that. Please tell us how we can improve. (Suggest an Improvement)

Codebulbs Writers Club

If you are passionate about web, open-source, or javascript and want to share, join Codebulbs writers club!

Join Program

Advertisement