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 为文本或图像赋予透明背景?


基础教程推荐
- 响应更改 div 大小保持纵横比 2022-01-01
- 在for循环中使用setTimeout 2022-01-01
- 角度Apollo设置WatchQuery结果为可用变量 2022-01-01
- 在 JS 中获取客户端时区(不是 GMT 偏移量) 2022-01-01
- 悬停时滑动输入并停留几秒钟 2022-01-01
- 我什么时候应该在导入时使用方括号 2022-01-01
- Karma-Jasmine:如何正确监视 Modal? 2022-01-01
- 动态更新多个选择框 2022-01-01
- 当用户滚动离开时如何暂停 youtube 嵌入 2022-01-01
- 有没有办法使用OpenLayers更改OpenStreetMap中某些要素 2022-09-06