Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Nextjs Image component srcSet attribute

I want to load different images for mobile viewport. Based on the Next.js doc passing srcSet does not work, so how can I do it?

Right now I can do it with CSS display property but I need something like the below:

<Image
   srcSet={{
      '/path/to/image-small.jpg': 320,
      '/path/to/image-medium.jpg': 640,
      '/path/to/image-large.jpg': 1024,
   }}
   src={data.smallImg.url}
   fill={true}
   alt=""
   className="rounded-[10px]"
/>
like image 686
Mohammad Saadati Avatar asked Apr 11 '26 15:04

Mohammad Saadati


1 Answers

The <Image /> component automatically generates your srcSet, but you can use deviceSizes in your config if you'd like.

If you want to load a different image for mobile, you could use the picture element. It also looks like Vercel has been working on support to get image props and use them in other elements (like <picture>).

like image 167
paulhm Avatar answered Apr 14 '26 01:04

paulhm



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!