I am feeling a bit puzzled about how to properly use the srcset
attribute within the HTML img
tag. Despite reading multiple tutorials online, it seems that things are not functioning as described in those resources. Let's begin with a simple example:
<img src="assets/img/software-developer.jpeg" alt="Some Stuff" srcset="assets/img/software-developer-300.jpeg 300w, assets/img/software-developer.jpeg 450w">
In this case, I have two images:
with dimensions of 450x450 pixelsassets/img/software-developer.jpeg
with dimensions of 300x300 pixelsassets/img/software-developer-300.jpeg
Despite my attempts using Chrome Developer Tools for testing, I observe that regardless of the screen size I select, the image being downloaded is always
assets/img/software-developer.jpeg
. This is evident when looking at the currentSrc
property under the Properties tab in Developer Tools.
Can someone shed some light on why this is happening? Ideally, I would like:
- for screens with a width >= 450px, download
assets/img/software-developer.jpeg
- for screens with a width < 450px, download
assets/img/software-developer-300.jpeg
Additionally, I am unsure about what occurs when the screen size is less than 300px:
If the smallest image available is
assets/img/software-developer-300.jpeg
with dimensions of 300x300 pixels, how can I adjust the code to allow it to resize and fit the viewport in such scenarios?
For instance, when using the Galaxy Fold device in Developer Tools with a width of 280 pixels, neither of the images fits the screen size: how should I update my srcset to enable resizing in this instance?
The internet contains abundant information relating to this topic, yet upon initial exploration, things appear somewhat perplexing. As a first step, I decided to conduct small tests focusing on srcset
to verify if the expected image is being downloaded. Unfortunately, it appears that the tag is not performing as anticipated. It is clear that I am overlooking something crucial, and I am hopeful that someone here can provide clarification.