I am trying to set my images to have a fixed pixel height and automatic width using object-fit: contain
.
How can I achieve this with the NextJS Image Component without using layout="fill"
so that the intrinsic width of the image (width: auto
) is preserved?
.wrapper {
display:flex;
flex-wrap: wrap;
}
.wrapper img {
width: auto;
height: 100px;
object-fit: contain;
}
<div class="wrapper">
<img src="https://picsum.photos/id/237/200/300"/>
<img src="https://picsum.photos/id/236/300/300"/>
<img src="https://picsum.photos/id/238/300/100"/>
<img src="https://picsum.photos/id/239/250/275"/>
<img src="https://picsum.photos/id/240/400/100"/>
<img src="https://picsum.photos/id/241/300/300"/>
</div>
The following code does not produce the desired result with the NextJS Image component:
<div className="wrapper">
<div className="image-wrapper">
<Image
layout="responsive"
width={image.width}
height={image.height}
src={image.src}
objectFit="contain"
/>
<div/>
<div className="image-wrapper">
<Image
layout="responsive"
width={image.width}
height={image.height}
src={image.src}
objectFit="contain"
/>
<div/>
</div>
.wrapper {
display:flex;
flex-wrap: wrap;
}
.image-wrapper {
width: auto;
height: 100px;
}