Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

picture (source) element VS img srcset attribute

The srcset attribute of the img element helps authors adapt their sites for high-resolution displays, to be able to use different assets representing the same image.

The picture element helps authors to control which image resource a user agent presents to a user, based on media query and/or support for a particular image format.

Both these give the author control over displaying images based on the device resolution;thus making the images responsive. So what is the main difference between them?

I did find some examples on the picture element draft, but still fail to understand the difference. Here are the examples:

Using srcset attribute:

<img src="pic1x.jpg" srcset="pic2x.jpg 2x, pic4x.jpg 4x"  alt="A rad wolf" width="500" height="500"> 

Using picture element:

<picture>   <source media="(min-width: 45em)" srcset="large.jpg">   <source media="(min-width: 18em)" srcset="med.jpg">   <img src="small.jpg" alt="The president giving an award."> </picture> 
like image 247
Abhishek Potnis Avatar asked Jun 10 '14 04:06

Abhishek Potnis


People also ask

What is the difference between src and Srcset?

The srcset attribute allows you to specify a list of image file URLs, along with size descriptions. Yo ualso need to still use the src attribute to identify a “default” image source, to be used in browsers that don't support srcset .

What is Srcset attribute in IMG tag?

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.

Do you need src with Srcset?

Yes it's valid. The src is used as a fallback if the browser doesn't support srcset. However if you are using it just for retina stuff you don't really need to use picturefill. I tend to just let it degrade back to the normal src if the browser doesn't support srcset.

Does Srcset load all images?

Each image in srcset can be taken. So if you want to control, what is used or not used, you need to use the picture element.


1 Answers

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. Hopefully in the future browsers would become smarter and smarter with selecting the appropriate image.

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.

like image 156
Salvador Dali Avatar answered Oct 06 '22 02:10

Salvador Dali