【问题标题】:Image Hover Overlay with opaque text带有不透明文本的图像悬停叠加
【发布时间】:2018-09-25 04:27:06
【问题描述】:

我要做的是创建一个图像悬停效果,当您将鼠标悬停在图像上时,叠加层是不透明的,上面是普通文本。现在,文本和背景是不透明的。

我尝试了很多方法,从 z-index 到使用 CSS 更改文本的不透明度,但似乎没有任何解决方法。

.text {
  color: white;
  font-size: 20px;
  position: relative;
  width: 425px;
  height: 300px;
  overflow: scroll;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  text-align: center;
  opacity: 1.0;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  opacity: 0;
  transition: .5s ease;
  background-color: #000000;
  z-index: -3.0;
}

.container:hover .overlay {
  opacity: .5;
}
<div class="container">
  <img src="http://oi63.tinypic.com/141obo6.jpg" alt="dog" class="image">
  <div class="overlay">
    <div class="text">
      <h3>.:.Appearance.:.</h3>
    </div>
  </div>
</div>

【问题讨论】:

  • 你必须发布你的覆盖(和悬停)css,这样我们也可以看到。这可能是您的问题所在。
  • .overlay { 位置:绝对;顶部:0;底部:0;左:0;右:0;高度:100%;宽度:100%;不透明度:0;过渡:0.5s 缓和;背景颜色:#000000; z 指数:-3.0; } 容器:悬停 .overlay { 不透明度:.5; }
  • 您可以编辑您的问题以添加代码吗?然后将其格式化为代码
  • 对不起,这是我第一次使用这个网站。

标签: html css


【解决方案1】:

问题出在属性opacity 中。 如果您为元素分配不透明度值,那么这也将应用于该元素内的所有子元素,在这种情况下,您的 .texth3

您可以通过使用rgba() 作为.overlay 的背景颜色并调整其中的不透明度来解决此问题,使其仅适用于该元素。

这是JSFiddle demo

body, html {
  height: 100%;
  width: 100%;
  margin: 0;
  /* background: #20262E; */
}

.text {
  color: white;
  font-size: 20px;
  position: relative;
  width: 425px;
  height: 300px;
  overflow: scroll;
  top: 50%;
  left: 50%;
  -webkit-transform: translate(-50%, -50%);
  -ms-transform: translate(-50%, -50%);
  transform: translate(-50%, -50%);
  text-align: center;
  opacity: 0;
}

.overlay {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  height: 100%;
  width: 100%;
  transition: .5s ease;
  background-color: rgba(0, 0, 0, 0);
  z-index: -3.0;
}

.container:hover .overlay {
  background-color: rgba(0, 0, 0, .5);
}

.container:hover .text {
  opacity: 1;
}
<div class="container">
  <img src="http://oi63.tinypic.com/141obo6.jpg" alt="dog" class="image">
  <div class="overlay">
    <div class="text">
      <h3>.:.Appearance.:.</h3>
    </div>
  </div>
</div>

【讨论】:

  • 成功了!!太感谢了!我做错了什么?我注意到您有一个用于悬停 .text 的容器,所以我错过了什么?
猜你喜欢
  • 1970-01-01
  • 2015-12-23
  • 2014-10-24
  • 1970-01-01
  • 2015-07-13
  • 2014-03-04
  • 1970-01-01
  • 2015-06-04
  • 2014-02-20
相关资源
最近更新 更多