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

原创文章 作者:月光光 2010年10月22日 16:38helloweba.com 标签:CSS3 

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

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

不管浏览器的高度怎么变化,我们要想让页底内容始终固定在底部,最终效果如:http://www.helloweba.net/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;} 
html, body {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:left;
width:0;
margin-top:-32767px;
}
</style>

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

<!--[if !IE 7]>
	<style type="text/css">
		#wrap {display:table;height:100%}
	</style>
<![endif]-->
声明:本文为原创文章,helloweba.net和作者拥有版权,如需转载,请注明来源于helloweba.net并保留原文链接:https://www.helloweba.net/javascript/69.html

3条评论

  • 可以

  • IE8好像不行

  • nice