【问题标题】:CSS hover effect doesn't appear on adding background imageCSS悬停效果在添加背景图像时不会出现
【发布时间】:2015-12-21 13:59:16
【问题描述】:

我在 div main-container 内的按钮上添加了 CSS 悬停效果。它里面还有一个background div,里面有一个layer div。问题是每次都有背景图像悬停效果不显示,但如果我删除背景图像,效果是可见的。我该如何解决? demo

.background {
    position: relative;
}

.background  img{
    max-width:100%;
	max-height:800px;
}

.layer {
    background-color: rgba(248, 247, 216, 0.7);
    position: absolute;
    top: 0;
    left: 0;
    width: 100%;
    height: 100%;
}
.btn {
	border: none;
	font-family: inherit;
	font-size: inherit;
	color: inherit;
	background: none;
	cursor: pointer;
	padding: 25px 80px;
	display: inline-block;
	margin: 15px 30px;
	text-transform: uppercase;
	letter-spacing: 1px;
	font-weight: 700;
	outline: none;
	position: relative;
	-webkit-transition: all 0.3s;
	-moz-transition: all 0.3s;
	transition: all 0.3s;
}

.btn:after {
	content: '';
	position: absolute;
	z-index: -1;
	-webkit-transition: all 0.3s;
	-moz-transition: all 0.3s;
	transition: all 0.3s;
}

.btn:before {
	speak: none;
	font-style: normal;
	font-weight: normal;
	font-variant: normal;
	text-transform: none;
	line-height: 1;
	position: relative;
	-webkit-font-smoothing: antialiased;
}

/* Button 1 */
.btn-1 {
	border: 3px solid #fff;
	color: #fff;
}

.btn-1f {
	overflow: hidden;
}

.btn-1f:after {
	width: 101%;
	height: 0;
	top: 50%;
	left: 50%;
	background: #000;
	opacity: 0;
	-webkit-transform: translateX(-50%) translateY(-50%);
	-moz-transform: translateX(-50%) translateY(-50%);
	-ms-transform: translateX(-50%) translateY(-50%);
	transform: translateX(-50%) translateY(-50%);
}

.btn-1f:hover,
.btn-1f:active {
	color: #0e83cd;
}

.btn-1f:hover:after {
	height: 75%;
	opacity: 1;
}

.btn-1f:active:after {
	height: 130%;
	opacity: 1;
}
.background {
    position: relative;
	width: inherit;
	height:inherit;
	background-image: url("http://www.wallpapereast.com/static/images/nature-wallpaper-desktop-308_ySxPe7m.jpg");
}
.main-container{
	width:100%;
	height:745px;
	position:relative;
}
.cities{position:absolute;bottom:0;}
<div class="main-container">
<div class="background">
  <div class="layer">
</div>
</div>
<div class="cities"><p>
<button class="btn btn-1 btn-1f"><a href="#">Bangalore</a></button>   
</p> 
</div>

【问题讨论】:

  • jsFiddle 没有为我打开。您可以通过edit提出问题并按 Ctrl + M 来创建本地 sn-p 吗?
  • 我不确定我是否理解问题所在。我知道背景图像总是在背景颜色之上。您可以尝试使用 z-index 修复它。
  • 如果他们有帮助,请考虑支持答案,以感谢人们为您的问题投入的时间和精力

标签: html css


【解决方案1】:

我检查了您的代码。 像下面这样在背景上给 Z-index 负值,

.background {
    background-image: url("http://www.wallpapereast.com/static/images/nature-wallpaper-desktop-308_ySxPe7m.jpg");
    height: inherit;
    position: relative;
    width: inherit;
    z-index: -1;
}

【讨论】:

    【解决方案2】:

    你可以这样做:

    .btn{
        z-index: 99;
    }
    .btn:hover a{
        color: #efefef;
    }
    

    【讨论】:

      【解决方案3】:

      给按钮一个z-index

      例如:z-index: 999;

      【讨论】:

        猜你喜欢
        • 2019-06-23
        • 1970-01-01
        • 2012-03-10
        • 1970-01-01
        • 2019-01-28
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多