Next Image not taking class properties

Before Next.js 12.2 Styling the next/image component’s margins this way doesn’t work in older Next.js versions. See relevant GitHub discussion for more details. Internally to the next/image component, the <img> element and the elements that wrap it have inline styles that override certain values passed through className. As a workaround, you can add a wrapper … Read more

How to use Image component in Next.js with unknown width and height

Yeah, you could use it with the layout=fill option you mentioned. On this case, you’re gonna need to set an “aspect ratio” for your images <div style={{ position: “relative”, width: “100%”, paddingBottom: “20%” }} > <Image alt=”Image Alt” src=”/image.jpg” layout=”fill” objectFit=”contain” // Scale your image down to fit into the container /> </div> You can … Read more

What is the best way to have a fallback image in NextJS?

You can create a custom image component that extends the built-in next/image and adds the fallback logic if the image fails to load by triggering the onError callback. import React, { useState } from ‘react’; import Image from ‘next/image’; const ImageWithFallback = (props) => { const { src, fallbackSrc, …rest } = props; const [imgSrc, … Read more