我正在使用类似下面代码的东西:
<div style="opacity:0.4; background-image:url(...);">
<div style="opacity:1.0;">
Text
</div>
</div>
我期望这会使背景的透明度为0.4,并且文本的透明度为100%。相反,它们两个的透明度都是0.4。
孩子们继承了不透明度。如果他们没有这种特性,那将是奇怪和不方便的。
您可以使用半透明的PNG文件作为背景图像,或者使用RGBa(a代表alpha)颜色作为背景颜色。
例如,50%淡化的黑色背景:
<div style="background-color:rgba(0, 0, 0, 0.5);">
<div>
Text added.
</div>
</div>
rgba(255,255,255,0.6)
相当于背景色的中性淡出。 - LateralFractal::before
或 ::after
来获得半透明的背景色,而且只需要一个容器即可实现。可以使用以下类似代码:<article>
Text.
</article>
article {
position: relative;
z-index: 1;
}
article::before {
content: "";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
opacity: .4;
z-index: -1;
background: url(path/to/your/image);
}
例子:
body {
background: red;
}
article {
position: relative;
z-index: 1;
}
article:before {
content: " ";
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100px;
opacity: .4;
z-index: -1;
background: url(https://31.media.tumblr.com/8ec07e49f33088c2e32c158ca4262eb2/tumblr_n5wav6Tz4R1st5lhmo1_1280.jpg);
}
<article>
Text.
</article>
z-index
值。CSS alpha透明度方法(在Internet Explorer 8中无效):
#div{background-color:rgba(255,0,0,0.5);}
根据您的选择使用透明的PNG图像作为背景。
使用以下CSS代码片段创建跨浏览器的半透明背景。这是一个示例,使用#000000
@ 0.4%不透明度。
.div {
background:rgb(0,0,0);
background: transparent\9;
background:rgba(0,0,0,0.4);
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#66000000,endColorstr=#66000000);
zoom: 1;
}
.div:nth-child(n) {
filter: none;
}
有关此技术的更多详细信息,请参见此处,该页面具有在线CSS生成器。
background-color
而不是background
。 - Wilt我会这样做
<div class="container">
<div class="text">
<p>text yay!</p>
</div>
</div>
CSS:
.container {
position: relative;
}
.container::before {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
background: url('/path/to/image.png');
opacity: .4;
content: "";
z-index: -1;
}
应该可以工作。这是在假设您需要半透明图像而不是颜色(您应该使用rgba)的情况下进行的。同时还假设您无法预先在Photoshop中更改图像的不透明度。
z-index
,而是 bgd
div 元素需要在 text
元素前面。 - T.Todua::before
伪元素上使用pointer-events: none;
而不是z-index: -1
将允许鼠标穿过伪元素单击实际元素,同时仍然显示伪元素在顶部。 - OXiGENtransparentize
。我发现这是最有用和易于使用的。transparentize(rgba(0, 0, 0, 0.5), 0.1) => rgba(0, 0, 0, 0.4)
transparentize(rgba(0, 0, 0, 0.8), 0.2) => rgba(0, 0, 0, 0.6)
了解更多: #transparentize($color, $amount) ⇒ Sass :: Script :: Value :: Color
.transbg{/* Fallback for web browsers that don't support RGBa */
background-color: rgb(0, 0, 0);
/* RGBa with 0.6 opacity */
background-color: rgba(0, 0, 0, 0.6);
/* For IE 5.5 - 7*/
filter:progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000);
/* For IE 8*/
-ms-filter: "progid:DXImageTransform.Microsoft.gradient(startColorstr=#99000000, endColorstr=#99000000)";}
<div id="background"></div>
<div id="bContent"></div>
设置所需的CSS不透明度和其他背景属性,并使用z-index属性 (z-index) 来样式化和定位bContent div。通过这种方式,您可以将div放置在背景div上方,而不会影响其不透明度。
另一种选择是使用RGBa。这将允许您嵌套div并仍然实现特定于div的不透明度。
最后一种选择是简单地在所选的图像编辑器中制作半透明的.png图像,将background-image属性设置为图像的URL,然后您就不必担心处理CSS并且失去嵌套div结构的功能和组织。
确保前景和背景的宽度和高度相同,或尝试使用上、下、左和右属性。
<style>
.foreground, .background {
position: absolute;
}
.foreground {
z-index: 1;
}
.background {
background-image: url(your/image/here.jpg);
opacity: 0.4;
}
</style>
<div class="foreground"></div>
<div class="background"></div>
<div style="background-image: url(...);"><div style="background-color: rgba(255, 255, 255, 0.7);">文字</div></div>
。 - Beamer