[英]How to size images by height and auto width with NextJS Image Component
I have images that I would like to have a fixed pixel height and auto width with object-fit: contain
.我有一些图像,我希望使用
object-fit: contain
具有固定的像素高度和自动宽度。
How does one achieve this behavior with NextJS Image Component?如何使用 NextJS 图像组件实现这一行为? I'd like to avoid
layout="fill"
as I would like the intrinsic width of the image ( width: auto
).我想避免
layout="fill"
因为我想要图像的固有宽度( width: auto
)。
.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 does NOT work as the above with NextJS Image component:以下对于 NextJS Image 组件不能像上面那样工作:
<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;
}
NextJS Image Component experimental support for "Raw" layout provided me with what I needed to achieve this. NextJS 图像组件对“原始”布局的实验性支持为我提供了实现这一目标所需的东西。
UPDATE : Experimental Raw layout is now next/future/image更新:实验性原始布局现在是next/future/image
https://github.com/vercel/next.js/issues/35493 https://github.com/vercel/next.js/issues/35493
.wrapper { display:flex; flex-wrap: wrap; }.img-wrapper { height: 100px; }.h-full { height: 100%; }.w-auto { width: auto; }.max-w-none { max-width: none; }.object-contain { object-fit: contain; }
<div className="wrapper"> <div className="img-wrapper"> <Image className="h-full w-auto max-w-none object-contain" alt={logo?.ariaLabel} src={src} layout="raw" width={width} height={height} /> </div> <div className="img-wrapper"> <Image className="h-full w-auto max-w-none object-contain" alt={logo?.ariaLabel} src={src} layout="raw" width={width} height={height} /> </div> <div className="img-wrapper"> <Image className="h-full w-auto max-w-none object-contain" alt={logo?.ariaLabel} src={src} layout="raw" width={width} height={height} /> </div> <div className="img-wrapper"> <Image className="h-full w-auto max-w-none object-contain" alt={logo?.ariaLabel} src={src} layout="raw" width={width} height={height} /> </div> <div className="img-wrapper"> <Image className="h-full w-auto max-w-none object-contain" alt={logo?.ariaLabel} src={src} layout="raw" width={width} height={height} /> </div> <div className="img-wrapper"> <Image className="h-full w-auto max-w-none object-contain" alt={logo?.ariaLabel} src={src} layout="raw" width={width} height={height} /> </div> <div/>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.