Mrs. Sri's room page takes 4.19 seconds on a phone before its first photo shows. After a fix, it takes 1.11 seconds. Same photos, same rooms, nothing looks different. The only change is how the images are sent.
This fix is called image optimisation: sending a photo as small as possible with no visible difference on screen, at the right size for the reader's screen, and at the right time. Picture Mrs. Sri sending room photos to a future guest by a courier who charges by the kilo. To pay less, she can pack them tighter (this is the file format). She can also send a print the size of the guest's frame instead of a billboard (this is srcset, several file sizes to choose from). The photo that will hang by the door right away is sent first (this is priority). The album that may never be opened waits (this is lazy loading). On the web, the courier's price is the guest's waiting time.
Four decisions for one photo
| Decision | Tool | On Mrs. Sri's room page |
|---|---|---|
| File format | <picture> with <source type> |
AVIF, then WebP, JPEG as the fallback |
| File size | srcset and sizes |
480, 800, 1200 pixels |
| When it loads | fetchpriority="high", loading="lazy" |
the first photo first, the gallery later |
| A box that holds still | width and height |
prevents CLS, from the Static & Dynamic Images lesson |
Format: measured on the same photo
Lossy formats (JPEG, WebP, AVIF) throw away detail the eye barely sees, so the file gets small. A lossless format (PNG) keeps every pixel as it is. SVG doesn't store pixels. SVG is a picture made of lines and shapes written as code, so it is sharp at any size.
We compressed one real 1200 × 900 pixel photo (a lake and mountains, like the view from Mrs. Sri's terrace) with real encoders, the programs that save a photo in one format. We chose the WebP and AVIF quality so their SSIM is no lower than the JPEG's. SSIM is a number from 0 to 1 that measures how close a result is to the original, and 1 means identical. So all four files look equally good, and only their sizes are compared. 1 KB = 1,000 bytes. The numbers in brackets are the difference from JPEG.
| Format (encoder, setting) | 1200 × 900 | 800 × 600 | 480 × 360 | SSIM at 1200 |
|---|---|---|---|---|
| PNG (lossless) | 1,483 KB | 669 KB | 244 KB | 1 |
| JPEG (libjpeg-turbo, quality 80) | 148 KB | 71 KB | 28 KB | 0.952 |
| WebP (cwebp, -q 79) | 108 KB (-27%) | 54 KB (-24%) | 22 KB (-22%) | 0.953 |
| AVIF (avifenc, -q 58) | 84 KB (-43%) | 43 KB (-39%) | 18 KB (-37%) | 0.954 |
- Google's study measured WebP 25 to 34% smaller than JPEG at the same SSIM. As WebP, our photo is 22 to 27% smaller, so the numbers agree.
- AVIF is the smallest: 37 to 43% smaller than JPEG. Chrome supports it since version 85, Firefox since 93, Safari on iOS since 16, and Edge since 121 (January 2024). Since Edge 121, every major browser supports it.
- For a photo, PNG is 10 times bigger than JPEG. PNG suits screenshots and sharp-edged pictures that must not change by a single pixel.
- Logos and icons use SVG. The Mist Homestay logo as an SVG is only 195 bytes and sharp at every size. As a 96 × 96 pixel PNG it is 1,128 bytes, and it has to be made again for each size.
So that older browsers that don't know AVIF or WebP still get a picture, use the <picture> tag. This tag wraps several file choices for one image. Each <source> is one choice, and its type attribute names the file's format:
<picture>
<source type="image/avif" srcset="terrace-room-1200.avif">
<source type="image/webp" srcset="terrace-room-1200.webp">
<img src="terrace-room-1200.jpg" width="1200" height="900" alt="Terrace Room: a double bed and a window facing the mountains">
</picture>
- The browser reads the
<source>elements from the top and uses the first one whosetypeit knows. A browser that knows AVIF stops at the first line. <img>is required, as the fallback for older browsers. The<img>is also where you write thealt,width,height,loadingandfetchpriorityattributes (the last two are explained below). A<source>only swaps the file the<img>shows.
srcset and sizes: the browser picks
Phone screens today have a DPR (device pixel ratio) of 2 or 3: every 1px of CSS is filled by 2 or 3 screen pixels. On a phone, the Terrace Room photo shows 327 CSS px wide. On a DPR 3 phone, that photo needs 327 × 3 = 981 screen pixels to look sharp. On a laptop, the same photo's place is 384px wide, and a regular laptop (DPR 1) needs 384 screen pixels. With one file for every screen, a small file makes the photo blurry on the phone, and a big file makes the laptop download a photo that's too big.
This code gives the browser three files of the Terrace Room photo to choose from:
<img
src="terrace-room-800.jpg"
srcset="terrace-room-480.jpg 480w, terrace-room-800.jpg 800w, terrace-room-1200.jpg 1200w"
sizes="(min-width: 1248px) 384px, (min-width: 768px) calc((100vw - 96px) / 3), calc(100vw - 48px)"
width="1200" height="900" alt="Terrace Room: a double bed and a window facing the mountains">
srcsetis the list of files.480wmeans "this file is 480 pixels wide".sizestells the browser how wide the photo's slot is (its place on the page). The browser picks a file before the CSS has loaded, so withoutsizesit doesn't know that width yet. The numbers come from the 12-column grid. The slot is 384px on screens from 1248px. On a tablet, it is (the screen width minus 96px of padding and gaps) divided by three. On a phone, it is the screen width minus 48px of padding. Conditions are read from the left, and the first one that matches is used.- The browser calculates slot × DPR, then takes the smallest file that is wide enough.
- Inside
<picture>, each<source>carries its ownsrcsetandsizes.
Guess first. Mr. Hadi's phone is 375px wide at DPR 3, the Terrace Room photo shows 327px wide, and there are three files: 480, 800 and 1200 pixels. Which one does the browser download?
We tested it in Chromium 147 (the base of Chrome and Edge), WebKit 26.4 (the base of Safari) and Firefox 146. All three gave the same result. The AVIF column is the downloaded file's size in AVIF:
| Screen | Photo slot | × DPR | Needs | Downloads | AVIF |
|---|---|---|---|---|---|
| Phone 375px, DPR 3 | 327px | × 3 | 981 | 1200w | 84 KB |
| Tablet 768px, DPR 2 | 224px | × 2 | 448 | 480w | 18 KB |
| Laptop 1280px, DPR 1 | 384px | × 1 | 384 | 480w | 18 KB |
| Laptop 1280px, DPR 2 | 384px | × 2 | 768 | 800w | 43 KB |
So the answer to the guess: 1200w. The 800 pixel file isn't enough for 981 screen pixels. Without srcset, there is only the 1200 file, and a regular laptop downloads it too (84 KB), though the 480 file (18 KB) is enough for its slot.
LCP: the first photo has to arrive first
LCP (Largest Contentful Paint) is the time until the largest element on the first screen (what you see before you scroll) has finished showing. LCP is rated good when it is at most 2.5 seconds, and poor when it is over 4 seconds. On a phone, that largest element is the Terrace Room photo.
We measured it in Chromium 147, on a 375px DPR 3 phone. The network was 1.6 Mbps with 150 ms (milliseconds) of delay, like the Slow 4G profile in Lighthouse (Google's tool for measuring page speed). The page held 45 KB of CSS, three room photos, and below them a gallery of 24 JPEG photos of 28 KB each:
| Page | Images downloaded | LCP |
|---|---|---|
| JPEG, every image loaded right away | 27 (1,121 KB) | 4.19 seconds (poor) |
| AVIF, every image loaded right away | 27 (930 KB) | 2.56 seconds |
AVIF, gallery and other rooms loading="lazy" |
13 (535 KB) | 1.11 seconds (good) |
AVIF, the Terrace Room photo loading="lazy" too |
13 (535 KB) | 2.86 seconds |
Bandwidth (the network's speed) is shared between every image being downloaded. The fewer images compete for it, the sooner the first photo arrives.
The fetchpriority="high" attribute tells the browser that this image matters. In our test its effect was small: LCP 1.12 seconds, almost the same as 1.11 seconds without the attribute. The reason: the Terrace Room photo is already first in the HTML, and Chrome already raises the priority of images on screen. On a busier page the effect can be large: web.dev reports Google Flights' LCP dropping from 2.6 to 1.9 seconds. Put it on one image only, the LCP image (the largest image on the first screen).
Lazy-load only below the fold
The fold is the bottom edge of the first screen. loading="lazy" tells the browser to delay the download until the image gets close to the screen. Here is one gallery photo:
<img src="gallery-sunrise.jpg" width="480" height="360" loading="lazy" alt="Sunrise over the hills">
- The browser decides how close. Chromium starts when the image is 1,250px from the screen on a fast connection, and 2,500px on a slow one. In our test (a 375px phone, the gallery starting 1,406px down), WebKit and Firefox downloaded none of the 24 gallery photos before the page was scrolled.
widthandheightare still required, so the box is there before the photo arrives.- The browser only requests a lazy image once it knows where the image sits on the page. It knows that only after the CSS has loaded and the browser has calculated the layout (where everything sits). For a photo on the first screen, that is too late: in the LCP table above, 1.11 seconds became 2.86 seconds.
⚠️ Why is the page still slow?
- PNG for a photo. The 1,483 KB PNG photo takes 7.2 seconds to download on a 1.6 Mbps network. Use AVIF or WebP with a JPEG fallback.
loading="lazy"on every image. The first photo has to wait too, and LCP goes up by 1.7 seconds. Lazy is only for what's below the fold.srcsetwithoutsizes. The browser assumes the slot is as wide as the screen (100vw), so a 1280px laptop downloads the 1200 file for a 384px slot.fetchpriority="high"on many images. If everything is important, nothing goes first.
TL;DR: Choose a format by measuring. On the same 1200 × 900 photo, JPEG is 148 KB, WebP 108 KB, AVIF 84 KB, PNG 1,483 KB. Google measured WebP 25 to 34% smaller than JPEG. Logos and icons use SVG (a picture made of lines and shapes). Write <picture> with <source type="image/avif">, <source type="image/webp">, then a JPEG <img> as the fallback: the browser uses the first format it knows. srcset lists the files (480w, 800w, 1200w), and sizes gives the width of the slot (the photo's place on the page). The browser takes the smallest file big enough for slot × DPR (how many screen pixels fill one CSS pixel): a 375px DPR 3 phone takes 1200w, a regular laptop 480w. LCP is the time until the largest element on the first screen (what you see before you scroll) shows, here a photo. That photo loads right away, may get fetchpriority="high" (a sign that it matters), and is never held back with loading="lazy". Only images below the fold (the bottom edge of the first screen) use loading="lazy", with width and height. In our test, phone LCP fell from 4.19 seconds (JPEG, everything loaded right away) to 1.11 seconds (AVIF, gallery lazy).
⚡ Format: measure, don't guess
Write:
<picture>
<source type="image/avif" srcset="terrace-room-1200.avif">
<source type="image/webp" srcset="terrace-room-1200.webp">
<img src="terrace-room-1200.jpg" width="1200" height="900" alt="Terrace Room: a double bed and a window facing the mountains">
</picture>
Meaning: One 1200 × 900 photo, at a quality that looks the same: JPEG 148 KB, WebP 108 KB, AVIF 84 KB, PNG 1,483 KB. <picture> wraps several file choices for one image. The browser uses the first <source> whose type (its file format) it knows, and the JPEG <img> is the fallback for older browsers. Logos and icons use SVG (a picture made of lines and shapes).
⚡ srcset and sizes: the browser picks
Guess first: Mr. Hadi's phone is 375px wide at DPR 3 (every CSS pixel is filled by 3 screen pixels). The photo slot (its place on the page) is 327px, and the files are 480, 800 and 1200 pixels. Which one is downloaded?
The 1200 pixel file. 327 × 3 = 981 screen pixels, and the 800 file isn't enough. The browser takes the smallest file that is big enough, not the closest one.
Meaning: srcset lists the files with their widths (1200w means 1200 pixels), and sizes gives the slot width. The browser calculates slot × DPR, then picks the smallest file that is wide enough.
⚡ The first photo is never lazy
Meaning: loading="lazy" makes the browser delay an image's download until the image is close to the screen. Use it only for images below the fold (the bottom edge of the first screen, before you scroll). In this lesson's test, phone LCP (the time until the largest photo on the first screen shows) fell from 4.19 seconds (JPEG, every image loaded right away) to 1.11 seconds (AVIF, gallery lazy). It rose again to 2.86 seconds when the first photo was lazy too. fetchpriority="high" (a sign that this image matters) goes on one image only: that largest photo.