【问题标题】:CSS z-index property doesn't workCSS z-index 属性不起作用
【发布时间】:2017-04-25 13:19:51
【问题描述】:

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

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

HTML

<div class="container">
  <div class="row">
     <div class="col-sm-6 left_part-padding">
         <div class="left_part">
           <div class="left_part_content">
             Left part
          </div>
         </div>
     </div> 
</div>

CSS

.container {
  margin-right: auto;
  margin-left: auto;
}


.left_part {
  height: 40vh;
  position: relative;
  z-index: 0;
  background-image: url(img/book2.jpeg);
  background-position: center center;
  background-repeat: no-repeat;
  background-size: cover;
  background-color: #000;
  box-shadow: 0 50px 60px 0 rgba(0,0,0,.35);
  border-radius: 10px;
}


.left_part:hover {
-webkit-filter: blur(5px);
filter: blur(5px);
-webkit-transition: .5s ease-in-out;
position: relative;
z-index:0;
}

.left_part_content:hover {
color: #fed136;
position: relative;
z-index: 2;
}

.left_part-padding, .right_part-padding, .bottom_part-padding {
padding-left: 10px;
padding-right: 10px;
}

.left_part_content {
  color: white;
  font-family: "Raleway", Helvetica, Arial, sans-serif;
  text-transform: uppercase;
  font-size: 1.2rem;
  font-weight: 400;
  letter-spacing: .300em;
  margin-right: -.300em;
  text-align: center;
  vertical-align: middle;
  line-height: 40vh;
  position: relative;
  z-index: 2;
}

非常感谢!

【问题讨论】:

  • blur 属性模糊了所有的元素和子元素,这样无法做出你想要的东西

标签: css z-index css-filters


【解决方案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);
}

【讨论】:

    【解决方案2】:

    看看这里:

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

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

    【讨论】:

      猜你喜欢
      • 2014-03-23
      • 1970-01-01
      • 1970-01-01
      • 2013-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多