【问题标题】:opacity background with no opacity text没有不透明文本的不透明背景
【发布时间】:2018-09-30 20:27:49
【问题描述】:

我正在尝试制作一个与屏幕一样宽但背景图像略微变暗的 div,但它会使任何重叠的文本也变暗。如何更改此设置,以便只有背景图像变暗而不是任何覆盖的文本?

.venue-header-text {
  font-size: 3.5em!important;
  font-weight: 700!important;
  color: white!important;
}

.inner-slider .slide {
  padding: 100px 0 100px;
  position: relative;
  background-size: cover;
  -moz-background-size: cover;
  -webkit-background-size: cover;
  background-repeat: no-repeat;
  background-position: center center;
  color: white!important;
}

.slide-overlay {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  background-color: rgba(0, 0, 0, 0.6)!important;
}
<div class="text-center">
  <section class="inner-slider hyr-section-height-override">
    <div class="slide" style="background-image:url('/site-data/articles/venue1/header.jpg');">
      <div class="container">
        <h2 class="venue-header-text">VENUE 1</h2>
      </div>
      <div class="slide-overlay"></div>
    </div>
  </section>
</div>

【问题讨论】:

  • 发布一个可运行,不工作的示例代码。您无法避免opacity 影响子元素,因此您必须创建一个结构,其中具有background-image 的元素没有子元素。
  • z-index 也许?
  • @connexo 问题不在于影响孩子的不透明度,而是堆叠问题......解释具有误导性

标签: html css opacity


【解决方案1】:

将文本放入.overlay 元素中。它的背景颜色将具有不透明度,但文本不会:

.venue-header-text{
    font-size:3.5em!important;
    font-weight:700!important;
    color:white!important;
}
.inner-slider .slide {
    padding: 100px 0 100px;
    position: relative;
    background-size: cover;
    -moz-background-size: cover;
    -webkit-background-size: cover;
    background-repeat: no-repeat;
    background-position: center center;
    color:white!important;
}

.slide-overlay {
    position: absolute;
     top: 0;
    left: 0;
    width: 100%;
    height: 100%;
    background-color: rgba(0,0,0,0.6)!important;
}
<div class="text-center">
<section class="inner-slider hyr-section-height-override">
<div class="slide" style="background-image:url('https://lorempixel.com/output/technics-q-c-640-480-2.jpg');">
<div class="container">

</div>
<div class="slide-overlay"><h2 class="venue-header-text">VENUE 1</h2></div>
</div>
</section>
</div>

【讨论】:

  • 完美答案谢谢(而且很明显!)。向帖子中的其他人道歉 - 并不是要误导我的问题。
猜你喜欢
  • 1970-01-01
  • 2016-12-02
  • 1970-01-01
  • 1970-01-01
  • 2016-09-17
  • 1970-01-01
  • 1970-01-01
  • 2011-12-15
  • 2018-06-22
相关资源
最近更新 更多