CSS+DIV讓頁尾始終底部
原文:CSS + DIV 讓頁尾始終底部
一 前言
經常設計頁面時用到三層DIV,頭DIV與腳DIV一般固定高度,而中間層DIV根據內容的多少,高度不定,我們經常希望但內容很少時,腳DIV保持在底部,當內容很多時,腳DIV被中間內容擠到下面,但仍然始終保持在底部。
二 position
position有四個引數:static | relative | absolute | fixed
position:static,意味元素沒有被定位,元素會出現在文件本該出現位置,是頁面元素預設的定位的方式,一般無需指定,除非想要覆蓋之前設定的定位。
position:relative,很明白,相對元素本該位置的偏移量
#nav{ position:relative; top:15px; left:20px; }
position:absolute,這時候元素已經脫離了文件,文件中已經沒有自己的本該的位置了,但我們可以通過left、bottom、left和right來規定其在文件中位置。
#nav{ postion:absolute; botton:0px; }
我們知道萬物都是相對的,元素進行上面設定了後就保證nav元素始終保持在底部了呢?nav元素離botton為0px,是哪個為參照物呢,是父級元素還還是瀏覽器呢,其實這裡分為兩種情況:
如果父級元素(父級元素的父級、父級的父級的父級……)設定postion時,則子元素此時相對的是父級的,所以當內容過多時,腳DIV不能被擠到底部去。
如果父級元素(父級元素的父級、父級的父級的父級……)沒有設定postion時,則子元素此時相對的是瀏覽器的,所以當內容過少時,腳DIV不能被調到底部去。
二 完整程式碼
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>CSS + DIV 讓頁尾始終底部</title> <meta name="generator" content="" /> <style type="text/css"> body{ margin:0; padding:0; font:12px/1.5 tahoma,arial,`Hiragino Sans GB`,5b8b4f53,sans-serif; position:absolute;width:100%;min-height:100%; } .content{ padding-bottom: 54px; } a:link,a:visited,a:active{color:#00749E;text-decoration:none;} a:hover{color:#000;text-decoration:underline;} #header{width:980px;height:69px;margin:0 auto;padding:0;} #header h1.blogtitle{font-family:Arial, Helvetica, sans-serif;margin:0;padding:10px 0 0;} #header p.desc{font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;margin:0;padding:0;} #top{background-color:#fff;background-image:url(images/topbck.gif);background-repeat:repeat-x;height:34px;} #ddnav{background-color:#312e2e;height:35px;margin:0;padding:0;} #nav{height:25px;width:980px;font-weight:700;margin:0 auto;padding:10px 0 0;} ul.nav{height:25px;line-height:25px;float:left;list-style:none;font-size:11px;text-transform:inherit;margin:0;padding:0;} ul.nav li{float:left !important;list-style-type:none;border-right:2px solid #312e2e;margin:0;padding:0;} ul.nav li a,ul.nav li a:link,ul.nav li a:visited{background:url(images/navsilver.gif) repeat-x;color:#818181;float:left;display:block;text-decoration:none;padding:0 15px;} ul.nav li a:hover,ul.nav li a:active {background:url(images/navblue.gif) repeat-x;color:#205387;text-decoration:none;} ul.nav li.current_page_item a{text-decoration:none;background:url(images/navblue.gif) repeat-x;color:#fff;} ul.nav li ul{float:left;margin:0;padding:0;} #footer{background-image:url(images/footerbck.gif);background-repeat:repeat-x;clear:both;height:24px;margin:0;padding:0;position:absolute;bottom:30px;width:100%;} #footerbox{color:#fff;background-color:#312e2e;height:30px;line-height:30px !important;margin:0;padding:0;clear:both;position:absolute;bottom:0px;width:100%;} .footer{width:980px;color:#fff;height:30px;line-height:30px !important;margin:0 auto;padding:0;} .footer a:hover{text-decoration:underline;color:#fff;border:none;} .footer a:link,.footer a:active,.footer a:visited{text-decoration:underline;color:#25aacd;border:none;} #top{background-color:#fff;background-image:url(images/topbck.gif);background-repeat:repeat-x;height:34px;} </style> <script type="text/javascript"> function aboutFunc(){ window.document.getElementById("about").style.display=""; } function indexFunc(){ window.document.getElementById("about").style.display="none"; } </script> </head> <body> <div id="header"> <h1 class="blogtitle"> <a href="http://www.cnblogs.com/chenyuming507950417/">助你軟體工作室</a> </h1> <p class="desc">二十八年,我用青春修行</p> </div> <div id="ddnav"> <div id="nav"> <ul class="nav"> <li><a href="#" onclick="indexFunc();">主頁</a></li> </ul> <ul class="nav"> <li><a href="#" onclick="aboutFunc();">關於</a></li> </ul> </div> </div> <div id="top"> </div> <div class="content"> <div id="post-e0a6ac53-c204-4919-9fda-2021397c40b8"> <p><p class="MsoNormal" style="margin-bottom: 7.5pt; text-align: center; mso-pagination: widow-orphan;" align="center"> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">麥子,你是怎樣長進我的眼睛</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span> </strong><strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">你站立的土地一直站著我的雙腳</span> </strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">我們同根而生,是夥伴</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">只是我比你在泥土上住得更久</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">我一輩子是生活的奴隸,為了生計</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">一年一次,我用鋒利的鐮刀收割</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">你的麥芒對著天,從沒刺傷我</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">為讓我拿鐮的手有更多的力氣</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">你在一盤磨裡走很長的路</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">把自己碾的粉身碎骨</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">我是在飢餓時學會了感恩</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">端起一碗飯就想你熟了的膚色</span> </strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">想起在泥土上來回行走的季節</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">麥子,你年年回來吧,我年年愛你</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"></span></strong> </p></p> </div> <div id="about" style="display:none;"> <p><p class="MsoNormal" style="margin-bottom: 7.5pt; text-align: center; mso-pagination: widow-orphan;" align="center"> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">麥子,你是怎樣長進我的眼睛</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span> </strong><strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">你站立的土地一直站著我的雙腳</span> </strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">我們同根而生,是夥伴</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">只是我比你在泥土上住得更久</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">我一輩子是生活的奴隸,為了生計</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">一年一次,我用鋒利的鐮刀收割</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">你的麥芒對著天,從沒刺傷我</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">為讓我拿鐮的手有更多的力氣</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">你在一盤磨裡走很長的路</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">把自己碾的粉身碎骨</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">我是在飢餓時學會了感恩</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">端起一碗飯就想你熟了的膚色</span> </strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">想起在泥土上來回行走的季節</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong> <strong><span style="font-size: 14pt; font-family: 宋體;" lang="ZH">麥子,你年年回來吧,我年年愛你</span></strong> <strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"></span></strong> </p></p> </div> </div> <div id="footer"> </div> <div id="footerbox"> <div class="footer"> <a href="http://www.cnblogs.com/chenyuming507950417/">助你軟體工作室</a> 二十八年,我用青春修行 © Copyright 2014 </div> </div> <body> </html>
二 參考文章
(1)http://blog.davidqiu.com/post/2013-06-17/40051753968
(2)http://www.cnblogs.com/ahjesus/archive/2011/11/26/2263986.html
相關文章
- CSS+DIV讓頁尾始終保持在頁面底部CSS
- 讓一個元素在網頁上跟隨網頁視窗大小變化始終保持上下左右居中網頁
- 以終為始:如何讓你的開發符合預期
- JS 網頁列印 頁邊距 頁首頁尾JS網頁
- css讓文字位於div的底部CSS
- Java 在PDF中插入頁首、頁尾Java
- 頁邊距和頁首頁尾的設定
- css+div分tabCSS
- css將元素固定於網頁底部CSS網頁
- CSS如何讓文字居於div的底部CSS
- mui 子頁面切換父頁面底部導航UI
- 終於知道為啥網頁不讓我複製貼上了!網頁
- css+div佈局案例CSS
- 【LaTeX入門】06、設定頁芯、頁邊距、頁首、頁尾
- 網頁底部版權資訊如何註明?網頁
- javascript如何使網頁如何回到底部JavaScript網頁
- 定製論文頁首頁尾設定需求
- 個性化設定Excel表格頁首頁尾Excel
- 如何設定個性化Excel頁首頁尾Excel
- word首頁不顯示頁碼怎麼設定 如何讓頁碼從第二頁開始顯示
- win10系統下word文件如何新增頁首頁尾_win10 word文件頁首頁尾怎麼設定Win10
- 【java web】--css+div總結JavaWebCSS
- 頁面沒有出現滾動條時底部懸浮顯示到最底部
- 禁止蒙層底部頁面跟隨滾動
- Excel2010設定奇偶頁頁首頁尾不同Excel
- 打造自己最酷最簡潔的CSS+DIV網頁佈局 (Js優化版)CSS網頁JS優化
- win10系統下如何在word任意頁尾設定頁碼_win10在word中設定頁尾頁碼的方法Win10
- Laravel 自定義 Markdown 電子郵件頁首和頁尾Laravel
- Excel2010表格中插入頁首頁尾Excel
- BIP rtf模板頁首頁尾中呼叫欄位值方法
- Excel2007怎麼插入頁首和頁尾?Excel2007插入頁首頁尾的方法步驟Excel
- 實現報表滾動到底部翻頁效果
- React 小案例 無線首頁底部導航元件React元件
- Sticky footers,元素一直在頁面底部
- laravel Hash::check 始終返回false?LaravelFalse
- recycleview聊天介面的從底部開始顯示的處理View
- css如何怎樣讓一個div永遠在底部CSS
- selenium 搜尋不到頁面最底部的按鈕