What is an srcset attribute in IMG tag and how to use it? - html

I want to know how could I start using the HTML srcset img attribute in my mobile apps. Or Is there any other jQuery plugin which helps me to solve image resolution problem.
<img srcset="banner-HD.jpeg 2x, banner-phone.jpeg 100w, banner-phone-HD.jpeg 100w 2x" alt="Banner Image" />

In short, Srcset is a new attribute which allows you to specify different kind of images for different screen-sizes/orientation/display-types. The usage is really simple, you just provide a lot of different images separating them with a comma like this: <img src="image.jpg" alt="image" srcset="<img> <descriptor>, ..., <img_n> <descriptor_n>">. Here is an example: srcset="image.jpg 160w, image2.jpg 320w, image3.jpg 2x"
This is a longer answer which explains things in more details.
Difference between srcset and picture. Both srcset and picture does approximately the same things, but there is a subtle difference: picture dictates what image the browser should use, whereas srcset gives the browser a choice. A lot of things can be used to select this choice like viewport size, users preferences, network condition and so on. The support for srcset is pretty good and almost all current browsers more or less support it. Situation with a picture element is a little bit worse.
Descriptors are just a way to show what kind of image is hidden behind the resource. There are various kinds of descriptors:
density descriptor. srcset="image.jpg, image-2X.jpg 2x"
The display density values—the 1x, 2x, etc.—are referred to as display density descriptors. If a display density descriptor isn’t provided, it is assumed to be 1x. Good variant to target retina displays.
width descriptor. srcset="image-240.jpg 240w, image-640.jpg 640w". I am sure this is self explanatory. The only problem is that by itself width descriptor is not really helpful. Why? read here
size descriptor, which only makes sense if you use width descriptor. srcset="image-160.jpg 160w, image-320.jpg 320w, image-640.jpg 640w, image-1280.jpg 1280w" sizes="(max-width: 480px) 100vw, (max-width: 900px) 33vw, 254px">. The instructions for the browser would look like this: (max-width: 480px) 100vw — if the viewport is 480 pixels wide or smaller, the image will be 100% of the viewport width. (max-width: 900px) 33vw — if the viewport is 480 pixels wide or smaller, this rule will never be reached because of the previous media condition. And 254px is when there is no media condition listed, the length is assumed to be a default value used when none of the other media conditions are met.
Just for completeness will add here that there is an image-set() attribute for a background image in CSS and some other helpful link here

Here is a detailed guide on srcset along with code samples.
srcset allows you to define a list of different image resources along with size information so that browser can pick the most appropriate image based on the actual device’s resolution.
Each comma-separated item in srcset has:
Image URL, e.g. http://ik.imagekit.io/demo/default-image.jpg or relative path /demo/default-image.jpg
An empty space
The actual width of the image or display density:
Either using display density descriptor, for example, 1.5x, 2x etc.
Or, using width descriptors, for example, 450w. This is the width of the image in pixels.
Using display density descriptor
The syntax for display density descriptors is straightforward. srcset provides a comma-separated list of image resources along with display density it should be used, for example1x, 2x etc.
<img src="image.jpg"
srcset="image.jpg,
image_2x.jpg 2x"
/>
Live demo - https://imagekitio.github.io/responsive-images-guide/srcset-density.html.
Using width descriptor
The syntax is similar to the display density descriptor, but instead of display density values, we provide the actual width of the image.
<img src="image.jpg"
srcset="small.jpg 300w,
medium.jpg 600w,
large.jpg 900w"
/>
This lets the browser pick the best image
Using width descriptor allows the browser to pick the best candidate from srcset based on the actual width needed to render that image on that particular display at runtime.
Note that display pixel density is also taken into account by the browser while calculating the required width. 😎
For example, assuming an image takes up the whole viewport width - On a 300px wide screen with DPR 2, the browser will pick medium.jpg because it needs a 300x2=600px wide image. On a 300px wide screen with DPR value 3, the browser will select large.jpg because it needs a 300x3=900px wide image.
Demo - srcset with width descriptor
Let see this in action with a live demo - https://imagekitio.github.io/responsive-images-guide/srcset-width.html.

Here is a good article on the srcset attribute and how to use it. srcet allows you to declare a set of images to be displayed on different viewport sizes. You just have to save and image at different resolutions e.g. banner-phone-HD.jpeg would be the highest resolution.
Exmaple:
<img alt="my awesome image"
src="banner.jpeg"
srcset="banner-HD.jpeg 2x, banner-phone.jpeg 640w, banner-phone-HD.jpeg 640w 2x">
The above would serve banner-phone.jpeg to devices with viewport width under 640px, banner-phone-HD.jpeg to small screen high DPI devices, banner-HD.jpeg to high DPI devices with screens greater than 640px, and banner.jpeg to everything else.
There are also other methods like CSS media queries you can use to produce the same effect.
I am not aware of any JQuery plugins which would help with this.

Related

Responsive full-width images and srcset

I have a site where I want some full-width images (they'll be cropped vertically, for a parallax look). I want to I've read a bunch of articles on srcset and sizes and tried a few things, but I can't figure out (a) whether I need to use "sizes" at all, and (b) especially without sizes, how the browser will choose which image to load.
For concreteness, here's an image spec:
<img src="/images/full/2c8n.jpg" srcset="
/images/320/2c8n.jpg 320w,
/images/640/2c8n.jpg 640w,
/images/1024/2c8n.jpg 1024w,
/images/full/2c8n.jpg">
This is in a full-width container div so the image should go edge to edge.
I'd like it to load the smallest one on mobile and the largest one (the /images/full one) on big desktops, but that doesn't seem to be happening. I've seen that it's possible to use x style as well, but I'm not sure how I'd choose which image to use with each scale factor, if that's even the right way to do it. Any idea what I'm doing wrong and what's the proper way to do this?
The documentation is pretty unclear about the sizes attribute. What I've been able to determine is that it only affects what size to render the image, and NOT image selection. So, sizes="(max-width: 500px) 50vw, 100vw" tells the browser:
if the viewport is up to 500px wide, render the image at 50% of the viewport (50vw)
if the viewport is 501px or larger, render the image at 100% of the viewport (100vw)
For the srcset attribute, the browser takes a look at your list of images, guesses at what size the slot is it should fill and then chooses the next size up. So, edge to edge as per your requirement, this is how it would choose which file to use:
Viewport width up to 319px -> /images/320/2c8n.jpg
VW 320px to 639px -> /images/640/2c8n.jpg
VW 640 to 1023 -> /images/1024/2c8n.jpg
VW 1024 and larger -> /images/full/2c8n.jpg
I've also read, but haven't verified yet, that it uses the first image it finds that fits the requirements between the src and srcset tags. The order of the attributes may make a difference. Again, read but not verified.

Responsive images - srcset and sizes attribute - how to use both correctly: device-pixel-ratio-based and viewport-based selection together?

I have read about this problem quite often so far and it also occurs for my own projects. Here is an introduction of what I have found out so far about the srcset and the sizes attribte.
There are two different possibilities on how to use the srcset-attribute (source w3c: http://w3c.github.io/html/semantics-embedded-content.html#device-pixel-ratio):
Device-pixel-ratio-based selection when the rendered size of the image is fixed
This is a simple and reliable way to use srcset. You simply say: If the device-pixel ratio of the target devicer is bigger than x, display this image with the following higher resolution.
The x descriptor is not appropriate when the rendered size of the
image depends on the viewport width (viewport-based selection), but
can be used together with art direction.
Example:
<img src="/uploads/100-marie-lloyd.jpg"
srcset="/uploads/150-marie-lloyd.jpg 1.5x, /uploads/200-marie-lloyd.jpg 2x"
alt="" width="100" height="150">
Viewport-based selection
This method allows you to display different image sizes depending on the size of your viewport. This is the method you are primarily using within your example.
The srcset and sizes attributes can be used, using the w descriptor,
to provide multiple images that only vary in their size (the smaller
image is a scaled-down version of the bigger image).
Simple example:
<h1><img sizes="100vw" srcset="wolf-400.jpg 400w, wolf-800.jpg 800w, wolf-1600.jpg 1600w"
src="wolf-400.jpg" alt="The rad wolf"></h1>
One step further: Using the sizes attribute
The default for Viewport-based selection and srcset is, that the image always has 100% width (100vw). The sizes attribute is giving the great possibility to tell the browser, how the width of an image is at a certain screen width.
The sizes attribute sets up the layout breakpoints at 30em and 50em,
and declares the image sizes between these breakpoints to be 100vw,
50vw, or calc(33vw - 100px). These sizes do not necessarily have to
match up exactly with the actual image width as specified in the CSS.
The user agent will pick a width from the sizes attribute, using the
first item with a (the part in parentheses) that
evaluates to true, or using the last item (calc(33vw - 100px)) if they
all evaluate to false.
Example:
<img sizes="(max-width: 30em) 100vw, (max-width: 50em) 50vw, calc(33vw - 100px)"
srcset="swing-200.jpg 200w, swing-400.jpg 400w, swing-800.jpg 800w, swing-1600.jpg 1600w"
src="swing-400.jpg" alt="Kettlebell Swing">
Here is the challange where I would be really glad if someone could enlighten me
Can I rely on srcset that a Client always load the correct image? Or is the actually loaded image also depending on processing power and internet connection speed as some people stated? I had complaints about retina devices which load lower resolution images.
How can I use both: Device-pixel-ratio-based and viewport-based selection together? Because for each possible size in sizes, I may want to define a retina image with 200% size as well as a non-retina image.
And furthermore: Does it make sense to use different images within srcset for different viewport sizes or is this a misuse of the srcset attribute? If it is possible to combine device-pixel-ratio-based and viewport-based selection, this should also be possible.
Can I rely on srcset that a Client always load the correct image?
The answer is NO. Moreso, you can never know the dimension of the image the user will upload unless you want to check that with a Javascript code and then restrict the user to upload the right dimension. But that will not be too user friendly.
Again, You might want to implement an algorithm to always resize the image to the particular size you want without distorting the quality, so you don't have to pass different imageurl to the srcset and just use the src attr. This can be an advantage to users with slow internet connection.
Does it make sense to use different images within srcset for different viewport sizes or is this a misuse of the srcset attribute?
The point is how many device Viewport do you want to handle in all. If you specify different image sizes and dimension for different view port, you might not able to target all at once especially when a new device is available that you didn't handle as at the time you were developing.

Explain `w` in `srcset` of image

I understand that the srcset is used to determine which image to load based on the dpi of the device. I came across this example on the Google Web Fundamentals which seems to use the w unit along with the vw unit which just happens to be the 1 % ofviewport width:
<img src="lighthouse-200.jpg" sizes="50vw"
srcset="lighthouse-100.jpg 100w, lighthouse-200.jpg 200w,
lighthouse-400.jpg 400w, lighthouse-800.jpg 800w,
lighthouse-1000.jpg 1000w, lighthouse-1400.jpg 1400w,
lighthouse-1800.jpg 1800w" alt="a lighthouse">
Help me understand what the unit of measure for this is and how I can use it?
According to MDN the w refers to:
a width descriptor (a positive integer directly followed by w).
The width descriptor is divided by the source size given in the sizes
attribute to calculate the effective pixel density.
Additionally, the W3 states:
The user agent will calculate the effective pixel density of each
image from the specified w descriptors and the specified rendered size
in the sizes attribute. It can then choose any of the given resources
depending on the user’s screen’s pixel density, zoom level, and
possibly other factors such as the user’s network conditions.
If the user’s screen is 320 CSS pixels wide, this is equivalent to
specifying wolf-400.jpg 1.25x, wolf-800.jpg 2.5x, wolf-1600.jpg 5x. On
the other hand, if the user’s screen is 1200 CSS pixels wide, this is
equivalent to specifying wolf-400.jpg 0.33x, wolf-800.jpg 0.67x, wolf-1600.jpg 1.33x. By using the w descriptors and the sizes
attribute, the user agent can choose the correct image source to
download regardless of how large the user’s device is.
Example 13 on the W3 page provides additional detail.
w is the intrinsic width of an image. It's the number you get when you right click the image and select Get Info on Mac, or Properties on Windows.
Why do you need to specify w? Because the first value of the srcset attribute only points to the link to load the image. It says nothing about the width of an image. The browser can't tell the dimension of an image until it has actually been loaded.
And why does the browser need to know the intrinsic width of an image?
Here is what browsers do when loading an image:
look at its device width
look at sizes and figure out which image size will be best to choose
look at the reference list in srcSet and choose the image whose size matches, or is bigger than the number above.
To borrow the sample code from MDN:
<img srcset="elva-fairy-480w.jpg 480w,
elva-fairy-800w.jpg 800w"
sizes="(max-width: 600px) 480px,
800px"
src="elva-fairy-800w.jpg"
alt="Elva dressed as a fairy">
A common practice is include the intrinsic image width in the image path. However, there is nothing that stops you from lying like this:
<img srcset="elva-fairy-480w.jpg 600w,
elva-fairy-800w.jpg 800w"
sizes="(max-width: 600px) 480px,
800px"
src="elva-fairy-800w.jpg"
alt="Elva dressed as a fairy">
This means that if a browser with a viewport width of 480px loads the page, the max-width: 600px condition will be true, and the browser has a slot of 480px wide to load image.
The image elva-fairy-480w.jpg will be loaded, as its intrinsic width (600w - inflated!) is closest to the slot size.
So there is probably no harm if you overstate the intrinsic width of your image.
What if you understate the intrinsic width?
<img srcset="elva-fairy-480w.jpg 280w,
elva-fairy-800w.jpg 800w"
sizes="(max-width: 600px) 480px,
800px"
src="elva-fairy-800w.jpg"
alt="Elva dressed as a fairy">
On a viewport width of 480px, the image elva-fairy-800w.jpg will be loaded instead because elva-fairy-480w.jpg is reported to be 280w and doesn't meet instruction in sizes.
So understating the intrinsic width of an image will result in the browser loading an overly large image, which defeats the very purpose of responsive iamges.
I haven't thought through all the ramifications from lying about the image's intrinsic width though.

Responsive img/srcset/sizes: Different jpg quality depending on device pixel density?

I am looking for a responsive image strategy that allows to serve different jpg quality based on the device pixel density.
On a small screen with high resolution, I would serve a low-quality but high-resolution jpg. On a big screen with low pixel density, I would serve a high-quality jpg, ideally matching the device resolution.
QUESTION:
Is this somehow possible with <img srcset=".." sizes=".." />?
Background / scenario
Different original images with different original dimensions.
Different image display contexts: As a gallery thumbnail, embedded in a blog post, in a modal box, full screen..
Responsive layout, with media queries that change the display size of those images, not necessarily proportional.
E.g. a what is displayed as a 100px thumbnail on desktop, might be displayed in full width on mobile.
High-resolution or "Retina" devices, with a resolution multiplier. On these I want many pixels, but low file size.
Solutions I'm considering
I think the promising approach for this is <img srcset=".." sizes=".."/>.
However, I am wondering if or how I should combine the x-descriptor and the w-descriptor.
The x-descriptor specifies a relative size. But relative to what? Both the original image size and the layout width of the <img> can vary between contexts and between viewports. The viewport reports a width for the media queries, but the actual pixel width can be 2x or 3x the reported viewport width, thanks to retina displays.
The w-descriptor specifies an absolute size. This sounds way better for image contexts that could be in thumbnail size on desktop, and full width on mobile - or vice versa.
Questions / Related
How could I serve different jpg quality depending on the pixel density on the device? (question as above)
Related question: Do srcset and sizes refer to device pixels or layout pixels?
You can do something like this
<picture>
<source media="(min-resolution: 1.5dppx)"
srcset="low-quality-high-res.jpg 2x">
<img src="high-quality-low-res.jpg" ...>
</picture>
In practice you probably want to have multiple sizes for each quality:
<picture>
<source media="(min-resolution: 1.5dppx)"
srcset="lq-500w.jpg 500w, lq-1000w.jpg 1000w"
sizes="100vw">
<img src="hq-250w.jpg"
srcset="hq-250w.jpg 250w, hq-500w.jpg 500w"
sizes="100vw" ...>
</picture>
(And change sizes as appropriate depending on context.)

Understanding srcset width values

I'm trying to understand how srcset width values work, but the basic example I have is throwing me off.
Given the code below, I expect the large image to display when the viewport is > 600px. However, it actually changes at 784px. I'm not sure why this is.
<!-- image changes to large variant at 784px instead of the expected 1024px -->
<img
src="small.jpg"
srcset="small.jpg 600w,
large.jpg 1024w"
alt="A test image">
Currently Chrome uses a geometric mean to choose between two image candidates. So it still uses the 600 image because 1024 is too big and therefore switches.
In an upcoming release Chrome will always switch to the higher image, if you have a low density device. On other devices Chrome will still use a a median.