问题:
Is it possible, using CSS only, to make the background
of an element semi-transparent but have the content (text & images) of the element opaque? 仅使用CSS,是否可以使元素的background
半透明,但元素的内容(文本和图像)不透明?
I'd like to accomplish this without having the text and the background as two separate elements. 我想在没有文本和背景作为两个独立元素的情况下完成此操作。
When trying: 尝试时:
p { position: absolute; background-color: green; filter: alpha(opacity=60); opacity: 0.6; } span { color: white; filter: alpha(opacity=100); opacity: 1; }
<p> <span>Hello world</span> </p>
It looks like child elements are subjected to the opacity of their parents, so opacity:1
is relative to the opacity:0.6
of the parent. 子元素似乎受到其父级的不透明度的影响,因此opacity:1
相对于父级的opacity:0.6
。
解决方案:
参考一: https://stackoom.com/question/3Ng0/如何使用CSS为文本或图像提供透明背景参考二: https://oldbug.net/q/3Ng0/How-do-I-give-text-or-an-image-a-transparent-background-using-CSS
来源:oschina
链接:https://my.oschina.net/u/4432649/blog/4463399