繁体   English   中英

有没有办法让 NextJS Image 组件按宽度填充其父容器并以高度响应以保持其尺寸?

[英]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?

我想并排放置两个 Next JS 图像组件并按宽度填充屏幕(当然,需要一些填充),但有一个响应高度来保留图像的尺寸。

这是我的代码:

<div className='flex flex-row p-4 border w-full justify-center'>

            <div className='w-1/2 h-screen border relative'>
                <Image src="{src}" layout='fill' />
            </div>

            <div className='w-1/2 h-screen border relative'>
                <Image src="{src}" layout='fill' />
            </div>
 </div>

显然,在父 div 上指定宽度和高度并在图像上填充不会保留其尺寸,所以我的直觉是使用 h-max 来设置宽度并让它填充图像高度,但在屏幕上它是空的因为没有东西可以填...

您可以使用的一件事/黑客是用div包装<Image>并向该 div 添加类(如hw )并将图像的布局设置为响应式,即layout=responsive 这将允许图像随着尺寸的变化而变化。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM