filter: alpha(opacity=90);
opacity: .9;
想要让 DIV 元素透明,但是当在该元素内添加另一个 DIV 元素时,它也会变为透明。
我只想让外部(背景)的 DIV 元素透明。如何做到?
Fiddle: http://jsfiddle.net/uenrX/1/
The opacity property of the outer DIV cannot be undone by the inner DIV. If you want to achieve transparency, use rgba
or hsla
:
Outer div:
background-color: rgba(255, 255, 255, 0.9); /* Color white with alpha 0.9*/
内部div:
background-color: #FFF; /* Background white, to override the background propery*/
编辑
由于您在问题中添加了 filter:alpha(opacity=90)
,我认为您也希望在(旧版本的)IE中获得可行的解决方案。这应该可以解决问题(对于最新版本的IE使用-ms-
前缀):
/*Padded for readability, you can write the following at one line:*/
filter: progid:DXImageTransform.Microsoft.Gradient(
GradientType=1,
startColorStr="#E6FFFFFF",
endColorStr="#E6FFFFFF");
/*Similarly: */
filter: progid:DXImageTransform.Microsoft.Gradient(
GradientType=1,
startColorStr="#E6FFFFFF",
endColorStr="#E6FFFFFF");
我使用了渐变滤镜,始于相同的 start-
和 end-color
,使得背景不显示渐变,而是一个纯色。颜色格式以 ARGB 十六进制格式表示。我编写了一个 JavaScript 片段将相对不透明度值转换为绝对 alpha-hex 值:
var opacity = .9;
var A_ofARGB = Math.round(opacity * 255).toString(16);
if(A_ofARGB.length == 1) A_ofARGB = "0"+a_ofARGB;
else if(!A_ofARGB.length) A_ofARGB = "00";
alert(A_ofARGB);
我曾经也遇到过同样的问题,这是我提出的解决方案,非常简单!
创建一个小的1像素 x 1像素透明图片,并将其保存为 .png 文件。
在您的 DIV 的 CSS 中使用以下代码:
background:transparent url('/images/trans-bg.png') repeat center top;
记得将文件路径更改为您的透明图像。
我认为这个解决方案在所有浏览器中都有效,除了IE6,但我没有测试过。
只需不为该 div 包含背景颜色,它就会变成透明的。
我不知道这是否有所改变。但从我的经验来看,嵌套元素的最大不透明度等于其父级元素。
这意味着:
<div id="a">
<div id="b">
</div></div>
Div#a has 0.6 opacity
div#b has 1 opacity
如果 #b 在 #a 内,则其最大不透明度始终为 0.6。
如果 #b 的不透明度为 0.5,则实际上它的不透明度为 0.6*0.5 == 0.3。
.modalBackground
{
filter: alpha(opacity=80);
opacity: 0.8;
z-index: 10000;
}
background-image:url('image/img2.jpg');
background-repeat:repeat-x;
使用一些内部图片并使用它。
这是不可能的,因为透明度会被子节点继承,你无法避免这种情况。如果只想让父元素透明,你需要通过定位元素(绝对定位)和它们的 z-index 属性来实现。
<div id="divmobile" style="position: fixed; background-color: transparent;
z-index: 1; bottom:5%; right: 0px; width: 50px; text-align:center;" class="div-mobile">