简体   繁体   English

如何防止透明背景颜色混合?

[英]How can I prevent transparent background-color blending?

I have the following code:我有以下代码:

 .wrapper { background-color: rgba(0,0,255,1.0); } .inner { background-color: rgba(255, 0, 0, 0.5); }
 <div class="wrapper">some text<span class="inner">other text</span></div>

My intention was that the <span> would have a light red color but instead I got red blended with the wrapper blue.我的意图是<span>将具有浅红色,但我将红色与包装蓝色混合。
Is there any easy way to make this work as I expected?有什么简单的方法可以按我的预期工作吗?

you can put the same span just behind the actual span , but with a white background-color.您可以将相同的span放在实际span后面,但背景颜色为白色。 However, it's not a sustainable solution, and you should not use rgba color to do that.然而,这不是一个可持续的解决方案,你不应该使用 rgba 颜色来做到这一点。

Due to the use of rgba(255,0,0,0.5);由于使用了rgba(255,0,0,0.5); you have an alpha option which causes semi transparency, your best bet is to simply use rgb(255,0,0);您有一个导致半透明的 alpha 选项,最好的办法是简单地使用rgb(255,0,0); without the alpha or optionally set your alpha to 1 rgba(255,0,0,1);没有 alpha 或可选地将您的 alpha 设置为 1 rgba(255,0,0,1);

Keep another element that is a wrapper to your span .保留另一个作为span包装器的元素。 and give it background white .并给它背景white

 .wrapper { background-color: #5555ff; position: relative; padding: 20px; } .inner { background-color: rgba(255, 0, 0, 0.5); display:block; } .fake{ display:inline-block; background: #fff; }
 <div class="wrapper"> some text <div class="fake"> <span class="inner">other text</span> </div> </div>

Alternate solution:替代解决方案:

You can give this color #ff8080 to your span which does not have transparency.您可以将此颜色#ff8080赋予不具有透明度的span It will give the same effect.它会产生相同的效果。

Best way is probably to just use a color without opacity, other than that you could just wrap the highlight color with a white background wrapper...最好的方法可能是只使用没有不透明度的颜色,除此之外,您可以用白色背景包装纸包裹高光颜色......

 .wrapper { background-color: rgba(0,0,255,1.0); } .inner { background-color: rgba(255, 0, 0, 0.5); } .inner-wrap { background-color: #FFF; }
 <div class="wrapper">some text<span class="inner-wrap"><span class="inner">other text</span></span></div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM