DEV Community

Cover image for Responsive fix for the Next.js Image component

Responsive fix for the Next.js Image component

Felix Häberle on May 06, 2021

Currently, layout="responsive" requires you to set width and height which you don't want to set because you don't know what the aspect ratio is goi...
Collapse
 
shriekdj profile image
Shrikant Dhayje •

thanks dude

Collapse
 
ivanjrmc profile image
Ivan Jeremic • • Edited

Any reason you use curly braces in className? It is just a string so you can use just a string without curly braces. Good article this helped me fixing a problem I had!

Collapse
 
markwilliams profile image
Mark Williams •

I'm guessing it's so it's easier to refactor to using string interpolation.

Collapse
 
orkhangg profile image
Orkhan Aliyev • • Edited

layout="fill" is deprecated. use fill={true} instead as follows:
<div className={'image-container'}>
<Image src="IMAGE_SOURCE" className="image" fill={true} alt="ALT_TEXT"/>
</div>

Collapse
 
yonmoyonmo profile image
Yeo, Wonmo •

thank you man

Collapse
 
jonm01 profile image
Jon Mathew •

With the above fix backdrop-filter: blur(5px); stopped working on a div that overlays on the image. Issue is only with Chrome and Firefox but on Safari it works

Collapse
 
paymanbaseri profile image
Payman Baseri •

stackoverflow.com/a/68838835/7757900

with a little changes

Collapse
 
studywithdesign profile image
studywithdesign •

I have used layout = "responsive" with width = {500} height = {270}

Collapse
 
fkrautwald profile image
Frederik Krautwald •

That was very helpful. I was struggling to hide and show images responsively. Now, I got a couple of great image components to use.

Collapse
 
hveveris profile image
Haris •

Thanks, very useful! XD

Collapse
 
pablowbk profile image
Pablo •

Thanks! been struggling with refactoring a component that was using "responsive", this hgave me the perfect grounds to find a solution. cheers!

Collapse
 
joshuatuscan profile image
Joshua Tuscan •

This saved me from a layout hell. Thanks so much!

Collapse
 
sameersmr profile image
SAMEER MATHAN •

thanks man

Collapse
 
hamishjohnson profile image
Hamish •

This destroys the cumulative layout shift features of Next/Image priority tag