

4 Ways to Optimise Images in Your Website Like a Pro
source link: https://blog.bitsrc.io/4-ways-to-optimise-images-in-your-website-like-a-pro-f73cb66ff4ac
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

4 Ways to Optimise Images in Your Website Like a Pro
Users get bored before a TikTok ends, why will they wait for your site to load?
Images are one of the most important factors in a webpage. People remember websites by brand and imagery, not text.
37% of your users will bounce if your website takes more than 5 seconds to load! It’s therefore, important that you do whatever you can to reduce load times and improve performance.
So with that in mind, let’s look at some useful techniques to optimise images.

Combine image formats
There are several types of image formats — JPG, JPEG, PNG, WEBP, AVIF etc. I suggest utilising WebP or AVIF for your images, depending on the browsers you want to support. WebP and AVIF are the most modern image formats, and they deliver high quality, with a small size.
I suggest utilising WebP or AVIF for your images, depending on the browsers you want to support.
These are the supported browsers for the WebP format. WebP is largely supported across all major browsers.
And here is the same again for AVIF format:
You could use WebP across all browsers, but then you’d be missing out on decreased load times in Chrome and Safari if you don’t use AVIF (about 66.6% people use this).
We can use the picture
element to show AVIF by default, and if it isn’t supported, we show the WebP format behind the scenes:
Note: If you want to combine more formats, you can add more source
elements for your formats.
Lazy Loading
Lazy loading is the process of showing an image only when it appears in the viewport.
Say there are 4 images, two on the home page, and the user has to scroll to reach the other 2 images. Initially, we load only the first two images, and when the user scrolls down, we load the other 2 images. This helps decrease the initial page load time.
There are three ways to load images lazily:
1. Loading Attribute
The img
tag has an attribute called loading
, which is super handy. All we have to do is set it to lazy
! Here’s an example:
Unfortunately, it’s not widely supported:
2. Intersection Observer
The Intersection Observer is a JavaScript API that triggers a callback when an element scrolls into viewport.
We can set the src
of the img
from JavaScript when the image is visible, which causes the browser to load the image.
Here’s an example:
I like using Intersection Observer as it is supported by all major browsers, and it is fairly easy to implement.
3. Scroll Events
This is implemented using the scroll
, resize
, orientationChange
, events in the browser.
The reason we are watching for these events as they trigger a change in the layout of the images. When one of these are triggered, we look at all unloaded images, and see which ones are in the viewport. If it’s in the viewport, we set the src
from the data-src
attribute.
I don’t like this method, as it’s complicated, and not very clean. But, if I had to support IE, I would utilise this method.
Fixed Image Dimensions
CLS or Cumulative Layout Shifts are caused due to the browser recalculating the layout once the image is loaded. The browser is unsure of the dimensions of the image before it’s loaded, so once it is loaded, the browser has to make space for the image. This increases the users waiting time, which is bad.
An easy solution to this is specifying the dimensions using the width
and height
attributes. This instructs the browser to reserve space for the image. When the image loads, the browser no longer has to recalculate the layout to fit the image.
Here’s an example:
<img src="img.png" width="300" height="300">
Compressed Images
This may seem obvious, but there are so many websites with bad image compression.
A useful tool I like to use squoosh.app. It lets you compare your original image, with your new one while testing out different sizes and formats.
Responsive Images
About 50% of users browse the internet using the mobile. Please, make your websites responsive. It’s so irritating when an image is breaking the layout!
We can use the srcset
property on the img
element, to use different images for different devices. The browser does some calculations based on the viewport width and pixel density. Based on this, the browser and chooses the best size of the image.
Below demonstrates how you would implement this:
TLDR;
The 4 ways to optimise images are:
- Combining Image Formats
- Lazy Loading
- Image Compression
- Responsive Images
Thank you for taking the time to read my post. If you found it useful, be sure to let me know in the comments or by leaving claps. You can also follow me here on Medium and also on Twitter.
Build optimized Image components with Bit
Microfrontends are a great way to speed up and scale app development, with independent deployments, decoupled codebases, and autonomous teams.
Bit offers a great developer experience for building component-driven Micro frontends. Build components, collaborate, and compose applications that scale. Our GitHub has over 14.5k stars!

Learn more
Recommend
-
44
README.md GameMode GameMode is a daemon/lib combo for Linux that allows games to request a set of optimisations be temporarily applied to the host OS. The design has a c...
-
33
Written by Harry Roberts on CSS Wizardry . When it comes to network performance, there are two main limiting factors that will slow you down: bandwidth and latency . B...
-
30
I'm very interested in performance. I'm not sure I can explain the underlying reasons for it. I'm easily frustrated at slow services and programs, and it seems like
-
7
John Fremlin's blog: How to optimise Common Lisp under SBCL: the garbage collector (draft)Waiting for updates: connectedPosted 2009-12-21 11:42:00 GMTWith Common Lis...
-
9
When a client requests a web page, the browser begins to download the html page and parses it. If an external resource is found it begins to request that resource in a parallel request. This means the the complete loading of the page...
-
9
How to change culture to optimise product designGoing from Good to Great — Talk by Louisa Laughton-Scott, Product Designer at Treatwell, at UX for Change London“I gave this talk just befo...
-
10
nishantpainter/personal-kanban: 📌 Personal Kanban Board is an agile project management tool that helps you visualise your work, limit your work-in-progress (WIP) and to craft & optimise your work flow to get the maximum output. To achieve th...
-
12
Resize and optimise images on upload with ActiveStorage has_many :codes Tips and walkthroughs on web technologies and digital life
-
10
John Fremlin's blog: How to optimise Common Lisp under SBCL: introductionWaiting for updates: connectedPosted 2009-12-21 11:44:00 GMTPeople occasionally ask me vague...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK