如何覆盖孩子的不透明度 [英] How to override opacity for a child
问题描述
我试图将不透明度文本4的不透明度重置为1.0,其中容器的不透明度设置为0.3。
我读到,我可以直接使用设置文本:color:
rgba(255,255,0,1);
但这不会为我工作。是否有办法实现我的目标?
I'm trying to reset the opacity to 1.0 for 'Demo text 4' where its container has opacity set to 0.3. I read that I could set the text directly using: color: rgba(255, 255, 0, 1); but this will not work for me. Is there a way to achieve my goal?
<!DOCTYPE html>
<html lang="en">
<head>
<style>
#text_holder {
background: blue;
width: 500px;
height: 200px;
}
#text_holder2 {
background: blue;
width: 500px;
height: 200px;
color: rgba(255, 255, 0, 1);
}
#alpha_30 {
opacity: 0.3;
color: #ff0000;
}
#alpha_100 {
color: #ff0000;
}
</style>
</head>
<body>
<div id="alpha_100">
<div id="text_holder">
<p>Demo text 1</p>
</div>
</div>
<div id="alpha_30">
<div id="text_holder">
<p>Demo text 2</p>
</div>
</div>
<div id="alpha_30">
<div id="text_holder">
<p>Demo text 3</p>
</div>
<div id="text_holder2">
<p>Demo text 4</p>
</div>
</div>
</body>
</html>
推荐答案
您不能。
如果使用纯背景颜色,则使用rgba。
If you use a plain background-color, then yes use rgba instead.
#text_holder {
background:rgba(0, 0, 255,1);
width: 500px;
height: 200px;
}
#text_holder2 {
background: rgba(0, 0, 255,1);;
width: 500px;
height: 200px;
color: rgba(255, 255, 0, 1);
}
#alpha_30 > div {/* select child */
/*opacity: 0.3;*/
background:rgba(0, 0, 255,0.3);/* give opacity to bg color only */
color: #ff0000;
}
#alpha_100 {
color: #ff0000;
}
对于一个图像作为背景,你可以假装是不透明的, - 颜色在rgba。如果你想要不透明度为0.3的背景,然后做1 -0.3 = 0.7设置你的rgba不透明度。
For an image as background, you may fake is opacity by using the main background-color in rgba. if you want an opacity of 0.3 for background, then do 1 -0.3 = 0.7 to set your rgba opacity.
<div class="bg-img">
<p class="text_holder"> some text</p>
</div>
.bg-img {
background:url(http://lorempixel.com/100/100/abstract);
}
.bg-img .text_holder {
background:rgba(255,255,255,0.3);/* here white cause body as white background */
}
这些都是解决方法:两者的DEMO(测试底部的bg图片):http://codepen.io/anon/pen/yGgpz
These are work around : DEMO of both (bg image at bottom of test): http://codepen.io/anon/pen/yGgpz
这篇关于如何覆盖孩子的不透明度的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!