IE:外层filter,内层position:relative或absolute。

FF:外层不使用opacity,而用background:rgba()。

假设背景为黑色,70%半透明,对需要半透明的层用如下CSS

background-color: #000000; /* background color for IE */
filter: alpha(opacity=70); /* opacity filter for IE */
background-color: rgba(0, 0, 0, 0.7); /* for other browsers */

如果只做了这一步,在IE中子元素也会被用上滤镜而透明,所以对于不需要透明的子元素们需要改变层级使之不受滤镜作用,方法是明确做一个定位,即将position设置成relative,absolute或fixed中的一种。至于另外两种:static,其实也就是默认值,是不定位的,而inherit会直接被IE无视掉

position: relative; /* or absolute/fixed */

[html] view plaincopy

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
    <html> 
     <head> 
      <title> New Document </title> 
      <meta name="Generator" content="EditPlus"> 
      <meta name="Author" content=""> 
      <meta name="Keywords" content=""> 
      <meta name="Description" content=""> 
      <style> 
            #wrapper 
            { 
                background: #369; 
                width:300px; 
                height:150px; 
            } 
            #div1 
            { 
                -moz-opacity: 0.3; /* FF 3.5以下 */ 
                opacity: 0.3; /* FF 3.5及以上 */ 
                filter: alpha(opacity=30); /* IE6及以上 */ 
                background: #fff; 
                width: 200px; 
                height: 50px; 
            } 
            #div2 
            { 
                background: rgba(255, 255, 255, 0.3) !important; /* IE无效,FF有效 */ 
                background: #fff; 
                filter: alpha(opacity=30); 
                width: 200px; 
                height: 50px; 
            } 
            #div2 span 
            { 
                position: relative; 
            } 
        </style> 
     </head> 
     
     <body> 
        <div id="wrapper"> 
            <div id="div1"> 
                <span>图层背景半透明,字体颜色也半透明</span> 
            </div> 
            <br /> 
            <div id="div2"> 
                <span>图层背景半透明,字体颜色不半透明</span> 
            </div> 
        </div> 
     </body> 
    </html> 

Copyright © 2014-2024 it689.com (京ICP备12032795号-2) 版权所有 Power by IT689