CSS布局:让页底内容永远固定在底部

时间 : 14-09-27 栏目 : css知识, html知识 作者 : admin 点击 : 2,539 次

我们在设计一些页面内容甚少的网页时(典型应用就是登陆页面),由于显示器的分辨率大,在正常情况下,假如页面内容高度小于浏览器高度时,页面底部以下会留下很大的空间...

先看示例:http://www.helloweba.com/demo/cssfooter/demo1.html

不管浏览器的高度怎么变化,我们要想让页底内容始终固定在底部,最终效果如:http://www.helloweba.com/demo/cssfooter/demo2.html

本文提供了一个CSS解决方案。

XHTML

<div id="wrap"> 
    <div id="main"> 
    主体 
    </div> 
</div> 
 
<div id="footer"> 
   这里是页底footer内容 
</div> 

CSS

<style type="text/css"> 
* {margin:0;padding:0;}  
htmlbody {height: 100%;} 
#wrap {min-height: 100%;} 
#main {overflow:auto; 
    padding-bottom: 60px;}  /* 必须使用和footer相同的高度 */ 
#footer {position: relative; 
    margin-top: -60px;  
    height: 60px; 
    clear:both; 
    background:#369}  
 
/*Opera Fix*/ 
body:before { 
content:""height:100%float:leftwidth:0margin-top:-32767px; 
} 
</style

注意,以上代码在IE6上根本不起作用,必须使用以下代码来解决IE6下的BUG。

<!--[if !IE 7]> 
    <style type="text/css"> 
        #wrap {display:table;height:100%} 
    </style> 
<![endif]--> 

除非注明,文章均为( admin )原创,转载请保留链接: http://www.pnyes.com/?p=30