如何使用CSS为文本或图像提供透明背景?

|▌冷眼眸甩不掉的悲伤 提交于 2020-10-08 02:23:25

问题:

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
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!