现在你可以像这样在CSS属性中使用rgba:
.class {
background: rgba(0,0,0,0.5);
}
0.5是透明度,根据你的设计更改数值。
rgba('black', 0.5)
? - phil294记住以下三个选项(您需要#3):
1)整个元素是透明的:
visibility: hidden;
2) 整个元素有一定的透明度:
opacity: 0.0 - 1.0;
3) 该元素仅有背景是透明的:
background-color: transparent;
要实现它,你需要修改元素的background-color
。
创建(半)透明颜色的方法:
The CSS color name transparent
creates a completely transparent color.
Usage:
.transparent{
background-color: transparent;
}
Using rgba
or hsla
color functions, that allow you to add the alpha channel (opacity) to the rgb
and hsl
functions. Their alpha values range from 0 - 1.
Usage:
.semi-transparent-yellow{
background-color: rgba(255, 255, 0, 0.5);
}
.transparent{
background-color: hsla(0, 0%, 0%, 0);
}
As of the CSS Color Module Level 4, rgb
and hsl
works the same way as rgba
and hsla
does, accepting an optional alpha value. So now you can do this:
.semi-transparent-yellow{
background-color: rgb(255, 255, 0, 0.5);
}
.transparent{
background-color: hsl(0, 0%, 0%, 0);
}
The same update to the standard (Color Module Level 4) also brought in support for space-separated values:
.semi-transparent-yellow{
background-color: rgba(255 255 0 / 0.5);
}
.transparent{
background-color: hsla(0 0% 0% / 0);
}
I'm not sure why would these two be any better than the old syntax, so consider using the a
-suffixed, comma-separated variants for greater support.
Besides the already mentioned solutions, you can also use the HEX format with alpha value (#RRGGBBAA
or #RGBA
notation).
That's contained by the same CSS Color Module Level 4, so it has worse support than the first two solutions, but it's already implemented in larger browsers (sorry, no IE).
This differs from the other solutions, as this treats the alpha channel (opacity) as a hexadecimal value as well, making it range from 0 - 255 (FF
).
Usage:
.semi-transparent-yellow{
background-color: #FFFF0080;
}
.transparent{
background-color: #0000;
}
您也可以尝试它们:
transparent
:
div {
position: absolute;
top: 50px;
left: 100px;
height: 100px;
width: 200px;
text-align: center;
line-height: 100px;
border: 1px dashed grey;
background-color: transparent;
}
<img src="https://via.placeholder.com/200x100">
<div>
Using `transparent`
</div>
hsla()
:
div {
position: absolute;
top: 50px;
left: 100px;
height: 100px;
width: 200px;
text-align: center;
line-height: 100px;
border: 1px dashed grey;
background-color: hsla(250, 100%, 50%, 0.3);
}
<img src="https://via.placeholder.com/200x100">
<div>
Using `hsla()`
</div>
rgb()
:
div {
position: absolute;
top: 50px;
left: 100px;
height: 100px;
width: 200px;
text-align: center;
line-height: 100px;
border: 1px dashed grey;
background-color: rgb(0, 255, 0, 0.3);
}
<img src="https://via.placeholder.com/200x100">
<div>
Using `rgb()`
</div>
hsla()
with space-separated values:
div {
position: absolute;
top: 50px;
left: 100px;
height: 100px;
width: 200px;
text-align: center;
line-height: 100px;
border: 1px dashed grey;
background-color: hsla(70 100% 50% / 0.3);
}
<img src="https://via.placeholder.com/200x100">
<div>
Using `hsla()` with spaces
</div>
#RRGGBBAA
:
div {
position: absolute;
top: 50px;
left: 100px;
height: 100px;
width: 200px;
text-align: center;
line-height: 100px;
border: 1px dashed grey;
background-color: #FF000060
}
<img src="https://via.placeholder.com/200x100">
<div>
Using `#RRGGBBAA`
</div>
这是一个使用CSS命名颜色的示例类:
.semi-transparent {
background: yellow;
opacity: 0.25;
}
这将添加一个背景,其为25%不透明(有颜色)和75%透明。
注意:不幸的是,不透明度会影响应用它的整个元素。因此,如果该元素中有文本,则文本也会变成25%不透明度。:-(
为了解决这个问题,可以使用rgba
或hsla
方法来指示所需背景“颜色”的透明度*。这允许您独立于元素中其他项目的透明度指定背景透明度*。
以下是3种设置75%不透明度(25%透明度)的蓝色背景的方法,而不影响其他元素:
background: rgba(0%,0%,100%,0.75)
background: rgba(0,0,255,0.75)
background: hsla(240,100%,50%,0.75)
background-color:rgba(0,0,0,0.5);
是最好的选择。
例如:background-color:rgba(0,0,0,opacity option);
innerHTML =
(嘿,至少比2012年使用的警报和document.write
好,但仍然没有DOM操作,除了jQuery教程之外,只有一点教程,在其中列出每章的相关方法,即使如此,它也只是选择 - 是的,他们确实列出了document.write
)。他们的XHR教程使用readyStateChange而不是onLoad(自IE7以来支持)。哦,还有他们在JSON中使用函数的解决方法?将它们字符串化以便稍后进行eval。 - John Dvorak.someDive{
background:transparent
}
rgba()
:
首先,我们操作背景颜色,并使用rgba。 .selector {
background-color: rgba(0, 0, 0, 0.3);
}
body {background-color: #0008f3;}
.container {
height: 100px;
width: 100px;
background-color: rgba(255,255,255,0.5);
}
<body>
<div class="container"></div>
</body>
完全透明 -> .youClass{background: rgba(0,0,0,0.001);}