gpt4 book ai didi

html - 悬停时更改图像背景颜色

转载 作者:太空宇宙 更新时间:2023-11-04 16:12:48 24 4
gpt4 key购买 nike

请看看这个例子,看看它应该是什么样子: Hover over image to change colour

请帮助我如何在悬停时更改图像背景颜色,这是我的代码,正如您在上面的示例中看到的,颜色应该是透明的

ul {
list-style: none;
}

li {
display: inline-block;
}

img {
filter: brightness(50%);
-webkit-filter: brightness(50%);
-moz-filter: brightness(50%);
-o-filter: brightness(50%);
-ms-filter: brightness(50%);
width: 246px;
height: 180px;
}

a:hover img {
background: blue;
}

.text {
font-size: 30px;
color: #fff;
position: absolute;
z-index: 1;
top: 0;
padding-left: 10px;
}
<div class="row">
<div class="col-lg-12">
<ul>
<li>
<div class="image">
<a href="#" title="Blog 7"><img src="http://mile.x3.rs/mile/hostel2hostel/img/bianca_capstick.png"></a>
</div>
<div class="text">
<p>Lorem Ipsum</p>
</div>
</li>
<li>
<div class="image">
<a href="#" title="Blog 8"><img src="http://mile.x3.rs/mile/hostel2hostel/img/coffe_keyboard.png"></a>
</div>
<div class="text">
<p>Lorem Ipsum</p>
</div>
</li>
<li>
<div class="image">
<a href="#" title="Blog 9"><img src="http://mile.x3.rs/mile/hostel2hostel/img/typing.png"></a>
</div>
<div class="text">
<p>Lorem Ipsum</p>
</div>
</li>
</ul>
</div>
</div>

最佳答案

使用 alpha channel 和负 z-index

  • 首先,您需要选择正确的元素:li:hover .image
  • 其次,您需要使用rgba 颜色来实现透明度

    li:hover .image{    
    background: rgba(0, 255, 255, 0.5);
    }
  • 第三,将实际图片推到后面:

    img {
    position: relative;
    z-index: -1;
    }

工作示例:

ul {
list-style: none;
}

li {
display: inline-block;
}

img {
filter: brightness(50%);
-webkit-filter: brightness(50%);
-moz-filter: brightness(50%);
-o-filter: brightness(50%);
-ms-filter: brightness(50%);
width: 246px;
height: 180px;
position: relative;
z-index: -1;
}

li:hover .image{
background: rgba(0, 255, 255, 0.5);
}
.image a{
height: 180px;
display: block;
}

.text {
font-size: 30px;
color: #fff;
position: absolute;
z-index: -1;
top: 0;
padding-left: 10px;
}
<!DOCTYPE html>
<html>
<head>
<title>TODO supply a title</title>
<meta charset="UTF-8">
</head>
<body>
<div class="row">
<div class="col-lg-12">
<ul>
<li>
<div class="image">
<a href="#" title="Blog 7"><img src="http://mile.x3.rs/mile/hostel2hostel/img/bianca_capstick.png"></a>
</div>
<div class="text">
<p>Lorem Ipsum</p>
</div>
</li>
<li>
<div class="image">
<a href="#" title="Blog 8"><img src="http://mile.x3.rs/mile/hostel2hostel/img/coffe_keyboard.png"></a>
</div>
<div class="text">
<p>Lorem Ipsum</p>
</div>
</li>
<li>
<div class="image">
<a href="#" title="Blog 9"><img src="http://mile.x3.rs/mile/hostel2hostel/img/typing.png"></a>
</div>
<div class="text">
<p>Lorem Ipsum</p>
</div>
</li>
</ul>
</div>
</div>
</body>
</html>

关于html - 悬停时更改图像背景颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33986068/

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