How do I give text or an image a transparent background using CSS?(如何使用 CSS 为文本或图像赋予透明背景?)
问题描述
是否可以仅使用 CSS 使元素的 background
半透明但元素的内容(文本和图像)不透明?
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?
我想在不将文本和背景作为两个独立元素的情况下完成此操作.
I'd like to accomplish this without having the text and the background as two separate elements.
尝试时:
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>
看起来子元素受制于其父元素的不透明度,因此 opacity:1
相对于父元素的 opacity:0.6
.
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.
推荐答案
要么使用半透明 PNG 或 SVG 图片或使用 CSS:
Either use a semi-transparent PNG or SVG image or use CSS:
background-color: rgba(255, 0, 0, 0.5);
这是一篇来自 css3.info 的文章,不透明度、RGBA 和妥协(2007-06-03).
Here's an article from css3.info, Opacity, RGBA and compromise (2007-06-03).
<p style="background-color: rgba(255, 0, 0, 0.5);">
<span>Hello, World!</span>
</p>
这篇关于如何使用 CSS 为文本或图像赋予透明背景?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何使用 CSS 为文本或图像赋予透明背景?


- 使用 iframe URL 的 jQuery UI 对话框 2022-01-01
- 如何显示带有换行符的文本标签? 2022-01-01
- 我不能使用 json 使用 react 向我的 web api 发出 Post 请求 2022-01-01
- 从原点悬停时触发 translateY() 2022-01-01
- 为什么悬停在委托事件处理程序中不起作用? 2022-01-01
- 在不使用循环的情况下查找数字数组中的一项 2022-01-01
- 如何向 ipc 渲染器发送添加回调 2022-01-01
- 是否可以将标志传递给 Gulp 以使其以不同的方式 2022-01-01
- 如何调试 CSS/Javascript 悬停问题 2022-01-01
- 为什么我的页面无法在 Github 上加载? 2022-01-01