html - 如何仅在背景图像而不是内部文本上应用 div 不透明度

标签 html css

我正在尝试应用一个简单的 css3 动画,并在文本跳转时对背景图像应用不透明度,但它会影响整个 div。

Image info - how it should look

Here is the jsfiddle link

这是主要的包装 div:

.movie_thumb_wrapper {
    float: left;
    line-height: 31px;
    background-color: #424755;
    -webkit-border-radius: 5px;
    -moz-border-radius: 5px;
    border-radius: 5px;
    overflow: hidden;
    height: 140px;
    width: 220px;
    background-size: 220px 140px;
    background-repeat: no-repeat;
       background-color:#1a1c26;
}

最佳答案

Vitorino Fernandes' answer 略有不同的方法将在文本和背景之间“嵌套”一个伪元素:

div {
  position: relative;
  height: 300px;
  width: 300px;
  display: inline-block;
  color:white;
}
div:before,
div:after {
  content: "";
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  transition:all 0.8s;
}
div:before {
  background: rgba(0, 0, 0, 0); /*this changes on hover - you might just want to change it here to get rid of the hover altogether*/
  z-index: -1;
}
div:after {
  z-index: -2;
  background: url(http://placekitten.com/g/300/300);
}
div:hover:before{
  background: rgba(0, 0, 0, 0.6);
  }
<div>Hover to see effect</div>


因此,根据您的 fiddle ,添加:

.movie_thumb_wrapper{
  position:relative;
}
.movie_thumb_wrapper:after {
  content: "";
  position: absolute;
  height: 100%;
  width: 100%;
  top: 0;
  left: 0;
  transition:all 0.8s;
    background:rgba(0,0,0,0.6);
    z-index:-2;
}

jsfiddle example

关于html - 如何仅在背景图像而不是内部文本上应用 div 不透明度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29119347/

相关文章:

javascript - 页面向下滚动时更改标题颜色

html - 复选框在单独的行上内联渲染

javascript - 用于启动/停止无限循环的按钮,每个函数之间有延迟

html - 居中图像组

css - 如何删除多余的 div 包装

html - 表格固定标题和带滚动条的第一列

css - 第一个 child 影响里面的每一个元素

javascript - 如何制作像 Facebook 的 "Chat/Event Ticker"那样可调整大小、可滚动的侧边栏?

html - Angular Material : Force footer to bottom

javascript - 提交表单时无需调用 javascript 函数即可进行 html 验证。