next.js - Next.js 以线性渐变为背景的图像组件
问题描述
在开发我的登录页面(使用 next.js )时,我决定使用这行代码作为一个部分的背景。
background-image: linear-gradient(to bottom, rgb(123, 8, 255, 0.9), rgb(123, 8, 255, 0.87)), url("/main background img.jpg");
但是,由于 next.js 允许您使用 Image 组件进行优化,我尝试以这种方式将相同的 img 作为背景
<PresentationHero2>
{/* Background */}
<Image
src="/main background img.jpg"
alt=""
layout="fill"
objectFit="cover"
/>
</PresentationHero2>
样式(带有样式组件)
export const PresentationHero2 = styles.div`
position: relative;
width: 100%;
height: 663px;
background: linear-gradient(to bottom, rgb(123, 8, 255, 0.9), rgb(123, 8, 255, 0.87));
display: flex;
align-items: center;
justify-content: center;
overflow: hidden;
`;
一切正常,问题是我看不到线性渐变,所以,我如何使用图像组件但同时使用背景颜色?,因为我唯一能看到的是图像,可悲的是:/
我希望你能帮我解决这个问题。谢谢你的时间 !
解决方案
您需要创建另一个 div,将其 z-index 设置为 1,并将位置设置为绝对,宽度和高度为 100%。这样,您的渐变将位于 Next/Image 之上。
<PresentationHero2>
<div className="radialBackground"></div>
<Image
src="/main background img.jpg"
alt=""
layout="fill"
objectFit="cover"
className="bgImage"
/>
</PresentationHero2>
.radialContainer{
z-index: 1;
position: absolute;
height: 100%;
width:100%;
background: linear-gradient(to bottom, rgb(123, 8, 255, 0.9), rgb(123, 8, 255,0.87));
}
.bgImage{
z-index: 0;
}