CSS z-index属性不起作用

时间:2017-04-25 13:19:51

标签: css3 z-index css-filters

我正在尝试使用“filter”属性为div(背景图片)添加悬停效果。但它已经影响了另一个div(我的文本)应该结束了。
我应用了z-index属性(没有忘记我的div的位置)但它不起作用。你会看到,我的文字也模糊不清。你能告诉我,我的代码有什么问题吗?





这是代码显示我的问题: https://codepen.io/Gillian-D/pen/qmqEQy





HTML

&#XA;&#XA; <预> &lt; div class =“container”&gt;&#xA; &lt; div class =“row”&gt;&#xA; &lt; div class =“col-sm-6 left_part-padding”&gt;&#xA; &lt; div class =“left_part”&gt;&#xA; &lt; div class =“left_part_content”&gt;&#xA;左边&#xA; &LT; / DIV&GT;&#XA; &LT; / DIV&GT;&#XA; &LT; / DIV&GT; &#XA;&LT; / DIV&GT;&#XA; &#XA;&#XA;

CSS

&#XA;&#XA;
 <代码> .container {&#xA; margin-right:auto;&#xA; margin-left:auto;&#xA;}&#xA;&#xA;&#xA; .left_part {&#xA;身高:40vh;&#xA;位置:相对;&#xA; z-index:0;&#xA; background-image:url(img / book2.jpeg);&#xA;背景位置:中心;&#xA; background-repeat:no-repeat;&#xA; background-size:cover;&#xA; background-color:#000;&#xA; box-shadow:0 50px 60px 0 rgba(0,0,0,.35);&#xA; border-radius:10px;&#xA;}&#xA;&#xA;&#xA; .left_part:hover {&#xA; -webkit-filter:blur(5px);&#xA; filter:blur( 5px);&#xA; -webkit-transition:.5s ease-in-out;&#xA; position:relative;&#xA; z-index:0;&#xA;}&#xA;&#xA ; .left_part_content:悬停{&#xA;颜色:#fed136;&#xA;位置:相对;&#xA; z-index:2;&#xA;}&#xA;&#xA; .left_part-padding ,.right_part-padding,.bottom_part-padding {&#xA; padding-left:10px;&#xA; padding-right:10px;&#xA;}&#xA;&#xA; .left_part_content {&#xA ;颜色:白色;&#xA; font-family:“Raleway”,Helvetica,Arial,sans-serif;&#xA; text-transform:uppercase;&#xA; font-size:1.2rem;&#xA; font-weight:400;&#xA;字母间距:.300em;&#xA; margin-right:-.300em;&#xA; text-align:center;&#xA; vertical-align:middle;&#xA; line-height:40vh;&#xA;位置:相对;&#xA; z-index:2;&#xA;}&#xA;  
&#xA;&#xA;

非常感谢!

&#xA;

2 个答案:

答案 0 :(得分:1)

当你对一个元素添加一个效果时,在大多数情况下他们的孩子也会受到影响,所以在这种情况下,你可以使用一个伪元素来background-image

Updated codepen

更改了CSS规则

.left_part {
  height: 40vh;
  position: relative;
  background-color: #000;
  box-shadow: 0 50px 60px 0 rgba(0,0,0,.35);
  border-radius: 10px;
}

.left_part::before {
  content: '';
  position: absolute;
  top: 0; left: 0;
  width: 100%;
  height: 100%;
  background-image: url(http://lorempixel.com/500/200/nature/1/);
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-color: #000;
  border-radius: 10px;
}

.left_part:hover::before {
  -webkit-filter: blur(5px);
  filter: blur(5px);
}

答案 1 :(得分:0)

看看这里:

https://codepen.io/anon/pen/EmNPVZ

我将文本部分元素从待模糊的背景div中取出,现在两者都具有相同的父级。接下来我通过不同的方式解决了居中问题,通常的方式(绝对位置,顶部和左边50%,transform: translate(-50%, -50%)。然后我将pointer-events: none;应用于文本图层,因此它下面的悬停可以说出来。我添加了一个影响文本的不同悬停规则 - 请参阅我的代码.HTH