糯米文學吧

位置:首頁 > 設計 > 網頁設計

頁腳始終保持在頁面底部的網頁佈局方法

導語:用CSS創建一個高度自適應佈局,如何保證頁腳(footer)在內容不超過一屏的情況下始終保持在佈局最下方是一個比較頭疼的事。下面就由本站小編為大家介紹一下頁腳始終保持在頁面底部的網頁佈局方法,希望對大家能有所幫助。

頁腳始終保持在頁面底部的網頁佈局方法

  步驟:

  1、為了讓瀏覽器識別高度100%我們需要先給 html 和 body 加上一個高度值,同時清除所有元素的 margin 和 padding。順便提一下,經過我的測試,html 和 body 的 height: 100%; 等於整個瀏覽器窗口的總高度,無論內容是否超過一屏。而它們下一級子元素 height: 100%; 則等於第一屏的.高度。如何,是不是有點不好理解?

* {

margin: 0;

padding: 0;

}

html, body {

height: 100%;

}

  2、因為上面提到的問題,所以為了讓佈局自適應高度,我們要加上 min-height: 100%;,雖然IE不支持這個屬性但是IE的 height: 100%; 有同樣的作用:

#wrapper {

min-height: 100%;

}

* html #wrapper {

height: 100%;

}

這樣,一個最簡單的最小高度滿一屏的自適應佈局就做好了。為了便於查看,我加了一些寬度和背景色修飾,如下:

* {

margin: 0;

padding: 0;

}

html, body {

height: 100%;

text-align: center;

font: 12px/1.4 Verdana, sans-serif;

background: #f00;

}

#wrapper {

width: 770px;

min-height: 100%;

background: #ccc;

margin: auto;

text-align: left;

}

* html #wrapper {

height: 100%;

}

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "">

<html xmlns="">

<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

<title>-建站學</title>

<style type="text/css">

/*<![CDATA[*/

* {

margin: 0;

padding: 0;

}

html, body {

height: 100%;

text-align: center;

font: 12px/1.4 Verdana, sans-serif;

background: #F00;

}

#wrapper {

width: 770px;

min-height: 100%;

background: #ccc;

margin: auto;

text-align: left;

}

* html #wrapper {

height: 100%;

}

#header {

background: Green;

height: 40px;

}

#sidebar {

float: left;

width: 200px;

background: Gray;

}

#content-box {

float: right;

width: 570px;

background: Olive;

}

#footer {

height: 50px;

background: Background;

width:770px;

margin: auto;

}

/*]]>*/

</style>

</head>

<body>

<p id="wrapper">

<p id="header">此處顯示 id "header" 的內容</p>

<p id="content-box">此處顯示 id "content-box" 的內容</p>

<p id="sidebar">此處顯示 id "sidebar" 的內容</p>

</p>

<p id="footer">此處顯示 id "footer" 的內容</p>

</body>

</html>