我们在布局的时候有这样一种情况,就是当页面高度没有超过屏幕高度的时候,footer 要固定到底部,而当页面超出满屏的高度的时候,footer 要随着高度走。下面我们就通过 CSS 实现这一效果:
下面的代码实现了当内容不满一屏时,底部内容始终位于显示屏的底部;当内容超过一屏时,则位于内容底部。
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>footer 始终居于底部</title>
<style type="text/css">
* { margin:0; padding:0; }
body { font:14px/1.8 arial; }
html, body, .wrap { height:100%; }
.wrap {
height:auto;
min-height:100%;
_height:100%;
background:#CCC;
color:#fff;
font-size:18px;
text-align:center;
}
.main { padding-bottom:80px; }
.footer {
position:relative;
height:80px;
line-height: 80px;
margin-top:-80px;
background:#333;
color:#fff;
font-size:16px;
text-align:center;
}
</style>
</head>
<body>
<div class="wrap">
<div class="main">
<h1>七七事变祭</h1>
<p>中华民族创辉煌,</p>
<p>倭寇菲佣心中慌。</p>
<p>为虎作伥傍老美,</p>
<p>东海南海滋事狂。</p>
<br />
<p>历史潮流不可挡,</p>
<p>中华儿女当自强。</p>
<p>警钟长鸣记国耻,</p>
<p>发展经济强国防。</p>
<br />
<p>七七事变 77 周年,勿忘国耻,振兴中华!!!</p>
<br />
</div>
</div>
<div class="footer">
<h1>页面高度不满,底部固定</h1>
</div>
</body>
</html>