Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is React.js removing the srcset tag on <img />?

When I have the srcset property on my <img /> tag, why doesn't it show up in the browser? It appears as through React.js is stripping it out.

<img src="/images/logo.png" srcset="/images/logo-1.5x.png 1.5x, /images/logo-2x.png 2x" />
like image 840
Kevin Ghadyani Avatar asked Sep 06 '25 13:09

Kevin Ghadyani


2 Answers

The solution is to use srcSet instead of srcset.

<img src="/images/logo.png" srcSet="/images/logo-1.5x.png 1.5x, /images/logo-2x.png 2x" />

Reference: https://facebook.github.io/react/docs/tags-and-attributes.html under HTML Attributes

like image 65
Kevin Ghadyani Avatar answered Sep 09 '25 04:09

Kevin Ghadyani


Another ugly solution using template literals:

<img
  alt=''
  src={require('../../assets/images/logo/logo.png')}
  srcSet={`
    ${require('../../assets/images/logo/[email protected]')} 2x, 
    ${require('../../assets/images/logo/[email protected]')} 3x
  `}
/>
like image 23
Milan Rakos Avatar answered Sep 09 '25 03:09

Milan Rakos