我一直在尝试用纯css在一个网站上做一个覆盖的悬停效果
<div class="col-md-4 port-show">
<img class="img-responsive" alt="" src="">
<h2 class=""></h2>
<ul>
<li></li>
<li></li>
</ul>
</div>
但我遇到了问题当我悬停在img,覆盖不会触发。我知道我必须改变做法,但就是不知道该怎么做。CSS:
:hover:after{
content: "";
z-index: 10;
display: block;
position: absolute;
height: 100%;
top: 0;
left: 0;
right: 0;
background: red;
border-radius: 6px;
border-color: transparent;
}
你可以看这里。也许这个办法对你有帮助。
http://codepen.io/anon/pen/VKmxZw.img-holder {
position: relative;
z-index: 1;
display: block;
width: 350px;
}
.img-holder img {
display: block;
width: 100%;
}
.img-holder:after {
content: " ";
width: 100%;
height: 100%;
background-color: red;
z-index: 2;
left: 0;
top: 0;
position: absolute;
opacity: 0;
}
.img-holder:hover:after {
opacity: 1;
}
<div class="col-md-4 port-show">
<span class="img-holder">
<img src="http://placehold.it/350x150" />
</span>
<h2 class=""></h2>
<ul>
<li></li>
<li></li>
</ul>
</div>
你可以按照这个来做你的叠加效果
@import url(http://fonts.googleapis.com/css?family=Open+Sans);
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: "Open Sans", sans-serif;
}
a {
color: inherit;
text-decoration: none;
}
h2 {
color: #c55;
padding: 50px 0;
text-align: center;
text-transform: uppercase;
}
.container {
background: url("http://static.pexels.com/wp-content/uploads/2015/03/fog-forest-haze-4827-524x350.jpeg");
background-size: cover;
background-position: center;
position: relative;
margin: auto;
width: 200px;
height: 200px;
cursor: pointer;
overflow: hidden;
}
.container:hover .overlay {
opacity: 1;
width: 100%;
height: 100%;
}
.container:hover .overlay span {
opacity: 1;
-webkit-transition: 1.3s ease;
transition: 1.3s ease;
}
.container .overlay {
background: rgba(0, 0, 0, 0.5);
position: absolute;
margin: auto;
width: 0px;
height: 0px;
top: 0;
right: 0;
bottom: 0;
left: 0;
opacity: 0;
-webkit-transition: .3s ease;
transition: .3s ease;
}
.container .overlay span {
color: #fff;
text-align: center;
position: absolute;
margin: auto;
width: 180px;
height: 30px;
line-height: 30px;
left: 0;
top: 0;
right: 0;
bottom: 0;
opacity: 0;
}
<h2>Image Overlay Hover Effect</h2>
<div class="container">
<a href="#">
<div class="overlay">
<span>Click to see more...</span>
</div>
</a>
</div>
一些简单的叠加代码:
<!DOCTYPE html>
<html >
<head>
<style type="text/css">
.pic{ width:190px;
height:190px; opacity: 1;
filter: alpha(opacity=100);
background: url(http://www.corelangs.com/css/box/img/duck.png) no-repeat; }
.pic:hover { opacity: 0.3; filter: alpha(opacity=30);}
</style>
</head>
<body>
<div class="pic">
</div>
</body>
</html>