Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

<img srcset="..."> versus <picture><source>?

I can't seem to discern a difference in function or meaning between using the picture element or just using the img element with the srcset attribute. I understand how they work; I just don't fully understand why or when to choose one over the other.

It looks like img element is lighter, cleaner code, but does it mean and do the same thing? I'm using this in my code to serve up different scaled versions of the same image:

<img src="default.jpg" 
srcset="image-800.jpg 800w, image-400.jpg 400w, image-200.jpg 200w" 
sizes="(max-width: 800px) 100vw, 12em" 
alt="something" />

It does exactly what I want as far as serving up the correct scaled image and it seems to work in Chrome, Firefox, Edge, and Opera.

I know that pretty much the same thing can be done with this code:

<picture>
<source srcset="image-800.jpg 800w, image-400.jpg 400w, image-200.jpg 200w" 
media="(max-width: 800px)" 
sizes="100vw" />
<source srcset="image-800.jpg 800w, image-400.jpg 400w, image-200.jpg 200w" 
media="(min-width: 800px)" 
sizes="12em" />
<img src="default.jpg" alt="something" />
</picture>

So, what's the difference functionally? Why would I use more code to do essentially the same thing?

Is there a semantic difference? What would that be?

From w3.org img:

An img element represents an image and its fallback content.

Also from w3.org picture:

The picture element is a container which provides multiples sources to its contained img element to allow authors to declaratively control or give hints to the user agent about which image resource to use, based on the screen pixel density, viewport size, image format, and other factors. It represents its children.

If the srcset attribute already does this for the img element, why do we need a picture element as a container? What am I missing? Is there a difference in semantic meaning?

I read one other post on here with a comment that suggested that srcset on the img element was simply new and not completely reliable crossbrowser. Is that still true? Was that the only difference?

like image 858
Jeremy Mallin Avatar asked Sep 18 '18 19:09

Jeremy Mallin


People also ask

What is the difference between the picture and IMG tags?

The <picture> tag in HTML is used to give flexibility to the web-developers to specify image resources. The <picture> tag contains <source> and <img> tags. The attribute value is set to load more appropriate image. The <img> element is used for the last child element of the picture declaration block.

What is IMG Srcset?

srcset defines the set of images we will allow the browser to choose between, and what size each image is. Each set of image information is separated from the previous one by a comma.

What is source Srcset?

Definition and Usage. The srcset attribute specifies the URL of the image to use in different situations. This attribute is required when <source> is used in <picture> .

Should I use Srcset?

A Priority Perspective. If performance (UX) is the priority, then use img@srcset or picture and source , whichever yields better results or uses less code, as long as image weight savings are bigger than the HTML payload increase (compared to just img ).


2 Answers

In your example, <picture> is useless, because you want to show the same image content everywhere.

<picture> is mandatory when you want to perform Art Direction, for example change the image width/height ratio when you hit a breakpoint, or crop the image to zoom in when you're on a small device.

like image 176
Nicolas Hoizey Avatar answered Oct 20 '22 09:10

Nicolas Hoizey


Alongside art direction a use case for using <picture> is when you want to serve a different filetype. For example, Chrome supports webp which has better compression than JPG. The only way to have Chrome pick a webp file is by providing it through <picture>.

<picture>
    <source srcset="image-800.webp 800w, image-400.webp 400w, image-200.webp 200w" 
type='image/webp' media="(max-width: 800px)" 
sizes="100vw" />
    <img src="default.webp" alt="something" />
</picture>
like image 25
bart Avatar answered Oct 20 '22 07:10

bart