日期:2013-06-03  浏览次数:21325 次

容器高度100%是经常用到的需求,任何容器都可以实现,而且不需求嵌套关系。

把body看作是一个容器,做为内部对象的上层标签,他的高度设置为100%是关键。

最基本的例子

* { margin:0; padding:0; border:0;}
html,body { height:100%;}
#box_2 { height:100%; background:#000;}

效果演示:http://www.rexsong.com/blog/atta ... _height100_base.htm

绝对定位堆叠效果

#box_1 { position:absolute; top:0; left:0; width:100%; height:50px; background:#f00; z-index:2;}
#box_2 { position:absolute; left:0; top:0; width:100%; height:100%; background:#000; z-index:1;}

效果演示:http://www.rexsong.com/blog/atta ... 301_height100_1.htm

纵向高度绝对大小效果

#box_1 { position:absolute; top:0; left:0; width:100%; height:30%; background:#f00; z-index:2;}
#box_2 { position:absolute; left:0; top:30%; width:100%; height:70%; background:#000; z-index:1;}

效果演示:http://www.rexsong.com/blog/atta ... 107_height100_2.htm

横向宽度绝对大小效果

#box_1 { position:absolute; top:0; left:0; width:30%; height:100%; background:#f00; z-index:2;}
#box_2 { position:absolute; top:0; right:0; width:70%; height:100%; background:#000; z-index:1;}

效果演示:http://www.rexsong.com/blog/atta ... 647_height100_3.htm

实际使用中,可有很多变化,轻松实现没有嵌套的绝对或绝对多列竖排、横排。

IE6.0和FF1.5测试通过