I am using NextJS Image component. I want the image to fill 100% width and take as much height as it needs to maintain the aspect ratio. I have tried many different variations of this code but it will not work unless I define a fixed height on the parent div.
export default function PhotoCard({ img }: Props) {
return (
<div className="relative w-full h-32 my-2">
<Image alt={img.title} src={img.url} layout="fill" objectFit="cover" />
</div>
);
}
If you load image
from external url
just remove width
and height
and use fill
<Image
loading={"lazy"}
fill
src={post.image}
/>