将 div 背景的不透明度设置为透明,而不改变文本

3

如何设置 hero-text 的背景透明度,而不影响其中的文字? 目前 hero-text 继承了 ion-slide 的透明度。

<style>
.slider {
    height: 100%;
    width: 100%;
}
.slide {
    background-size: cover; 
}
#one { 
    background: url(img/walkthrough-1.png) no-repeat;
    opacity: 0.5;
}
#two {
    background: url(img/walkthrough-2.png) no-repeat;   
}
#three {
    background: url(img/walkthrough-3.png) no-repeat; 
}
.splash {
    bottom: 0px;
}
.hero-text {
}
</style>

<ion-content class="splash" scroll="false">
    <ion-slide-box on-slide-changed="slideHasChanged($index)">
        <ion-slide id="one"><div class="hero-text">Test</div></ion-slide>
    </ion-slide-box>
</ion-content>
1个回答

1

如果你只需要调整 <ion slide id="xxx"> 的不透明度,那么我建议将你的 .hero-text div 分离出来,根据需要调整样式以保持设计正确。

利用 CSS,你可以调整背景颜色的不透明度 -- background-color: rgba(0, 0, 0, 0.5) -- 这将创建一个50%不透明的黑色背景颜色,并且不会影响任何子元素。但是,如果您要减少背景图像的不透明度,则唯一的方法是使用 opacity,这当然会影响子元素。

我在一个也使用了“hero image”的网站上使用了相同的概念,并希望背景图像淡入/淡出而不影响任何文本。您可能需要使用以下 CSS 来适当地放置所有内容:

.hero-wrapper {
  position: relative;
}

.hero-wrapper > .bg {
  background: ...;
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 100%;
  z-index: 0;
}

.hero-wrapper > .text {
  position: relative;
  z-index: 10;
}

这应该可以工作,虽然是手写的。如果您需要更多解释或提供一个可行的示例,请告诉我。谢谢 ~ 编辑 这是我的建议通过 Plnkr 的一个可行示例 :) Plnkr Demo

谢谢您的回复,能否请提供一个可用的示例? - methuselah

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接