CSS Sticky Footer: 完美的CSS绝对底部

<div id="wrap">
	<div id="main" class="clearfix">
		<div id="content">
		</div>
		<div id="side">
		</div>
	</div>
</div>
<div id="footer">
</div>

说明 : 使用这个布局的前提,就是footer要在总的div容器之外,footer使用一个层,其它所有内容使用一个总的层。如果确实需要到添加其它同级层,那这个同级层就必须使用position:absolute进行绝对定位。

CSS代码:

下面是主要的CSS代码,让你的底部可以位于窗口的最下面:

html, body, #wrap {height: 100%;}
body > #wrap {height: auto; min-height: 100%;}
#main {padding-bottom: 150px;}  /* 必须使用和footer相同的高度 */
#footer {position: relative;
    margin-top: -150px; /* footer高度的负值 */
    height: 150px;
    clear:both;}

说明 : 需要注意的就是#main的padding值、footer的高度和负margin值,需要保持一致。

就是这么简单,不过还没完。如果你的主体是使用悬浮布局,还得解决一些浏览器的兼容问题,这里使用的重点是为了Goolge Chrome。

对#main部份进行著名的 Clearfix Hack :

.clearfix:after {content: ".";
	display: block;
	height: 0;
	clear: both;
	visibility: hidden;}
.clearfix {display: inline-block;}
/* Hides from IE-mac \*/
* html .clearfix { height: 1%;}
.clearfix {display: block;}
/* End hide from IE-mac */

注: 该方案测试于两栏悬浮布局,兼容各大主流浏览器,包括 Google Chrome

猜你喜欢

转载自blog.csdn.net/shenyanwei/article/details/73041239