[英]Is there a way to make a NextJS Image component fill its parent container width-wise and respond with a height to preserve its dimensions?
[英]How to make NextJS Image component 100% height and width for its parent div?
作为标题,我正在努力让Image
表现得像img
,有谁知道如何让Image
像下面的例子一样实现img
?
<div style={{ width: "100vw", height: "100vh", display: "flex" }}>
{/* img */}
<div style={{ width: "50%", height: "100%", background: "red" }}>
<img
src="https://t4.ftcdn.net/jpg/03/00/94/69/360_F_300946931_kSR84OqudEhsmBZH47HU6ud7aZIDMjEx.jpg"
alt=""
style={{ width: "100%", height: "100%", objectFit: "cover" }}
/>
</div>
{/* Image */}
<div style={{ width: "50%", height: "100%", background: "blue" }}>
<Image
src="https://t4.ftcdn.net/jpg/03/00/94/69/360_F_300946931_kSR84OqudEhsmBZH47HU6ud7aZIDMjEx.jpg"
alt=""
width="1"
height="1"
layout="responsive"
objectFit="cover"
/>
</div>
</div>
#UPDATE这就是答案。
{/* Image */}
<div
style={{
width: "50%",
height: "100%",
background: "blue",
position: "relative",
}}
>
<Image
src="https://t4.ftcdn.net/jpg/03/00/94/69/360_F_300946931_kSR84OqudEhsmBZH47HU6ud7aZIDMjEx.jpg"
alt=""
layout="fill"
objectFit="cover"
/>
</div>
好的,我看到您希望图像采用其父元素的宽度和高度,您可以使用layout="fill"
而不是responsive
,这会导致图像扩展以填充其父元素。
像这样的东西:
<div style={{width: '100%', height: '100%', position: 'relative'}}>
<Image
alt='Mountains'
src='/mountains.jpg'
layout='fill'
objectFit='contain'
/>
</div>
给它的父母一个块的显示
并取出预设的宽高属性
<div style={{ width: "50%", height: "100%", background: "blue",display: "block" }}>
<Image
src="https://t4.ftcdn.net/jpg/03/00/94/69/360_F_300946931_kSR84OqudEhsmBZH47HU6ud7aZIDMjEx.jpg"
alt=""
layout="responsive"
objectFit="cover"
/>
</div>
你可以查看文档
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.