by 清泉
19. 十月 2009 12:19
一般来说,如果不需要继续父属性,只要加上position:relative就可以了。
具体来说就是下面这样:
html结构:
<div class="cnt">
<div class="bd">
content here<br/>
content here<br/>
content here<br/>
content here<br/>
content here<br/>
content here
</div>
<div class="ft"></div>
</div>
CSS:
.cnt{
width:45em;
overflow:hidden; /*必须的,否则ft的透明层将会偏出*/
color:#fff;
position:absolute;/*根据需要设定,不是必须的*/
filter:Alpha(opacity=20);/*IE下的透明度*/
_background:#000;/*IE下的背景色*/
_position:static;/*必须的*/
}
.bd{
position:relative;/*必须的,和父层的position:static对应*/
}
.ft{ /*FF下,做一个透明层,放在内容下面*/
background:#000;
position:absolute; /*必须*/
top:0;/*必须*/
left:0;/*必须*/
right:0;/*必须*/
bottom:0;/*必须*/
z-index:-1;/*必须*/
opacity:0.2;/*必须*/
*opacity:1; /*这个是为了应付其他浏览器*/
*background:transparent; /*IE下是完全透明的*/
}
分开分析:
IE下面,cnt层设了透明度,为了避免影响子层,父层position:static,子层position:relative;
FF下面,cnt层不设透明度,ft层设了透明度,并且让它放在内容下面并充满父层。
这样就实现了避免CSS透明度继承的效果,高!