gpt4 book ai didi

html - 如何使用css向元素添加渐变叠加?

转载 作者:行者123 更新时间:2023-11-28 00:45:32 29 4
gpt4 key购买 nike

我正在制作这个 page section 的 html 标记.正如您在这里看到的,它是三张图片。当我将鼠标光标放在其中一个上时,图像会移动并覆盖线性渐变。图片代码

代码

.image_row {
display: flex;
flex-direction: row;
justify-content: space-between;
}

.image_row img:hover,
.image_row img:focus {
transform: translate(-9px, -9px);
box-shadow: 10px 10px #95e1d3;
}
<div class="image_row">
<img src="image-1" height="250" width="380" />
<img src="image-2" height="250" width="380" />
<img src="image-3" height="250" width="380" />
</div>

我的问题是我不知道如何为图像制作渐变叠加。我尝试了以下代码,但没有带来任何效果:

.image_row img:hover,
.image_row img:focus{
transform: translate(-9px, -9px);
box-shadow: 10px 10px #95e1d3;
background-image: -moz-linear-gradient( 90deg, rgb(252,227,138) 0%, rgb(243,129,129) 100%);
background-image: -webkit-linear-gradient( 90deg, rgb(252,227,138) 0%, rgb(243,129,129) 100%);
background-image: -ms-linear-gradient( 90deg, rgb(252,227,138) 0%, rgb(243,129,129) 100%);
mix-blend-mode: normal;
opacity: 0.9;
}

最佳答案

您可以使用 CSS 添加一个伪元素来覆盖图像并在悬停/焦点上显示,这也具有渐变效果。

我将您的代码修改为以下示例:

.image_row {
position: relative;
display: flex;
flex-direction: row;
justify-content: space-between;
}

.image_row img:hover,
.image_row img:focus {
transform: translate(-9px, -9px);
box-shadow: 10px 10px #95e1d3;
}

img {
display: block;
}

.image {
position: relative;
}

.image::before {
content: '';
position: absolute;
top: 0;
left: 0;
right: 0;
bottom: 0;
z-index: 1;
}

.image:hover,
.image:focus {
transform: translate(-9px, -9px);
box-shadow: 10px 10px #95e1d3;
}

.image:hover::before,
.image:focus::before {
background-image: -moz-linear-gradient( 90deg, rgb(252, 227, 138) 0%, rgb(243, 129, 129) 100%);
background-image: -webkit-linear-gradient( 90deg, rgb(252, 227, 138) 0%, rgb(243, 129, 129) 100%);
background-image: -ms-linear-gradient( 90deg, rgb(252, 227, 138) 0%, rgb(243, 129, 129) 100%);
mix-blend-mode: overlay;
}
<!DOCTYPE html>
<html lang="en" dir="ltr">

<head>
<meta charset="utf-8">
<title></title>
</head>

<body>
<div class="image_row">
<div class="image">
<img src="http://via.placeholder.com/380x250" height="250" width="380" />
</div>
<div class="image">
<img src="http://via.placeholder.com/380x250" height="250" width="380" />
</div>
<div class="image">
<img src="http://via.placeholder.com/380x250" height="250" width="380" />
</div>
</div>
</body>

</html>

关于html - 如何使用css向<img>元素添加渐变叠加?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50787210/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com