Sub topic 14
Checking which image was chosen
This chapter assumes you can write srcset, sizes, and picture markup, and that you know the difference between CSS pixels and device pixels. Here you will learn how to confirm which file the browser actually downloaded and how to find out why it chose that file.
Why you need to check
The browser, not you, picks the file. Your markup supplies candidates and hints. A wrong sizes value, a missing variant, or a cached file can all lead to a choice you did not expect. The usual symptoms are a blurry image on a sharp screen, or a huge file on a small phone. Checking takes a few seconds and catches both.
Read currentSrc
Every img element has a currentSrc property. It holds the URL of the file the browser selected, whichever way it selected it: srcset, sizes, or a matching source inside picture.
const img = document.querySelector('img');
console.log(img.currentSrc);
console.log(window.devicePixelRatio);Run this in the browser console. devicePixelRatio is the number of device pixels per CSS pixel. A value of 2 means a 2x screen. Read the two values together with the width of the image on the page. On a 2x screen, an image shown 400 CSS pixels wide needs about 800 pixels of source width.
Two points to remember:
currentSrcis empty until the browser has chosen a file. Read it after the page has loaded, or after the image'sloadevent.- For an image inside
picture, query theimgelement. Thesourceelements have nocurrentSrc.
Use the Network panel
Open your browser's developer tools and go to the Network panel. Filter by Img, then reload the page.
- Find your image in the list.
- Check the file name, which shows which candidate was chosen.
- Check the size column, which shows the transferred bytes.
- Check the image's intrinsic size, which most tools show in the request's preview or in the Elements panel tooltip.
Then resize the window and reload. Most browsers keep a larger file they have already downloaded and do not fetch a smaller one when the window shrinks, so reload after each resize to see a fresh choice. Tick "Disable cache" in the Network panel so a cached file does not hide the result.
Test several conditions
Test the conditions your markup depends on, not only your own screen.
- Viewport width. Resize the window across each breakpoint named in
sizesand inmedia. - Pixel density. Use the device toolbar in the developer tools to emulate a different device pixel ratio.
- Format support. Check that the
typeorder you wrote gives the format you expect, such as AVIF before WebP before JPEG.
Common causes of a surprising result
| Symptom | Likely cause |
|---|---|
| The largest file loads on a small screen | `sizes` is missing, so the browser assumes `100vw`, or it overstates the display width |
| The same file loads at every width | The `w` descriptors do not match the real file widths, or only one candidate exists |
| A smaller file never loads after you shrink the window | The browser reused the larger file it had already downloaded |
| The wrong crop appears | The `media` conditions in `source` elements are in the wrong order, because the first match wins |
| The JPEG loads in a modern browser | The `type` value is misspelled, or an earlier `source` matched first |
Follow the check
Exercise
On a 2x screen, an image is displayed 500 CSS pixels wide. currentSrc points to a file that is 1600 pixels wide, and a 1000-pixel file exists in the srcset. What is the most likely problem, and what should you check first?
Show answer
The browser needs about 1000 pixels (500 x 2), so it should have chosen the 1000-pixel file. Check the sizes attribute first. It probably overstates the displayed width, for example by being missing and defaulting to 100vw. Also confirm the w descriptors match the real file widths and that the cache is disabled.
Exercise
You shrink the browser window and the Network panel still shows the large file. Does this mean your srcset is broken?
Show answer
Not necessarily. Browsers often reuse a larger image they already downloaded instead of fetching a smaller one. Reload the page with the cache disabled at the smaller width, then check currentSrc again.