2012-08-25 509 views
0

我一直試圖讓這個我該死的佈局工作了很長時間,現在它超級frusterating。我是CSS新手,所以請簡單,但我查了很多關於粘腳和伸展內容等的文章,但我仍然無法使用它。最低高度對我沒有任何幫助!如何讓頁面內容伸展並將頁腳粘貼到頁面底部?

Here is what i want my site to look like when it doesn't have enough content to fill a page.

And this is what i want it to look like when the content makes you scroll.

下面是HTML代碼:

<!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>Welcome to SilmanServer!</title> 
<link href="./main.css" rel="stylesheet" type="text/css" /> 

</head> 

<body> 

<div id="pageHeader"> 
<h1>SilmanServer</h1> 
<p>Aaron Silman's Personal Home Server </p> 
</div> 

<div id="navigation"> 
    <ul> 
     <li><a href="./index.html">Home</a></li> 
     <li><a href="./blog.html">Blog</a></li> 
     <li><a href="./projects.html">Projects</a></li> 
     <li><a href="./about.html">About Me</a></li> 
     <li><a href="./contact.html">Contact</a></li> 
    </ul> 
</div> 


<div id="main"> 

<div> 
<h2>What the hell?</h2> 
    <p> 
    This the project I embarked on around June of 2012 with an old computer that I had at home. My goal: To transform that old, useless HP Pavilion a6750y into a fully functioning webserver for this website! Along the way I also learned (X)HTML, CSS, Javascript, PHP, MySQL and a bunch of other things needed for designing and developing websites, as well as administrating my own server. 
    </p> 
</div> 

<div> 
<h2> Why? </h2> 
    <p> As mentioned before, I really wanted to learn how to build websites that function both on the client side and server side. I wanted to just play around and learn. So I started learning, beginning with the basics. I will also use this website as documentation, tracking my progress and noting a lot of important steps so if anyone (including myself - in case I forget) wants a guide to setting up a server and learning web design and development they an refer to <a href="./guide_to_the_web.html">this page </a> for a complete, idiot-proof, guide to the web from scratch. 
    </p> 
</div> 

</div> 

<div id="pageFooter"> 
    <p> This is a footer 
    </p> 
</div> 

</div> 
</body> 
</html> 

這裏是CSS:

/* Eric Meyer's Reset CSS v2.0 - http://cssreset.com */ 
html,body,div,span,applet,object,iframe,h1,h2,h3,h4,h5,h6,p,blockquote,pre,a,abbr,acronym,address,big,cite,code,del,dfn,em,img,ins,kbd,q,s,samp,small,strike,strong,sub,sup,tt,var,b,u,i,center,dl,dt,dd,ol,ul,li,fieldset,form,label,legend,table,caption,tbody,tfoot,thead,tr,th,td,article,aside,canvas,details,embed,figure,figcaption,footer,header,hgroup,menu,nav,output,ruby,section,summary,time,mark,audio,video{border:0;font-size:100%;font:inherit;vertical-align:baseline;margin:0;padding:0}article,aside,details,figcaption,figure,footer,header,hgroup,menu,nav,section{display:block}body{line-height:1}ol,ul{list-style:none}blockquote,q{quotes:none}blockquote:before,blockquote:after,q:before,q:after{content:none}table{border-collapse:collapse;border-spacing:0} 

/* Global Styles */ 

html{ 
    background: url(./images/sway.jpg); 
    background-attachment: fixed; 
    background-repeat::no-repeat; 
} 

body { 
    width: 80%; 
    margin: 0 auto; 
} 
/* pageHeader Styles */ 

#pageHeader{ 
    height: 100px; 
    background:#999; 
} 

#pageHeader h1 { 
    padding-left: 30px; 
    font-family:Arial, Helvetica, sans-serif; 
    font-size: 3.5em; 
    color:#F93; 
} 

#pageHeader p{ 
    padding-left: 40px; 
    font-family: "Times New Roman", Times, serif; 
    font-size: 2em; 
    color: white; 
} 

/* navigation Styles */ 

#navigation{ 
    float:left; 
    width:100%; 
    background: #0CF; 
    overflow: hidden; 
    position: relative; 
} 

#navigation ul{ 

} 

#navigation li{ 
    float: left; 
    margin: 0 20px; 
    font-size: 1.5em; 
} 

#navigation li a:link {color:#F60;} /* unvisited link */ 
#navigation li a:visited {color:##89ABFC;} /* visited link */ 
#navigation li a:hover {color:#3FF;} /* mouse over link */ 
#navigation li a:active {color:#009;} /* selected link */ 

/* main Styles */ 

#main{ 
    width: 80%; 
    margin: 0 auto; 
    border: 2px solid black; 
    background: #999; 
    color: #FFF; 
} 

/* pageFooter Styles */ 

#pageFooter{ 
    height: 100px; 
    background:#999; 
} 

#pageFooter h1 { 
    padding-left: 30px; 
    font-family:Arial, Helvetica, sans-serif; 
    font-size: 3.5em; 
    color:#F93; 
} 

#pageFooter p{ 
    font-family: "Times New Roman", Times, serif; 
    font-size: 2em; 
    color: white; 
} 

我能做些什麼來達到這種效果呢?

+1

歡迎來到SO!如果您希望人們幫助您,請避免使用粗言穢語,過長的代碼片段和未格式化的代碼,如果您確實需要粘貼一些代碼。更好的是,考慮提供一個鏈接到您的在線沙箱,或者在http://pastebin.me/或http://jsfiddle.net/或http://jsbin.com/或類似網站上設置一個準系統示例。 – Systembolaget

回答

8

你在找什麼是「粘腳」。

  • 要做到這一點的較舊的CSS方法要求您知道頁腳的確切高度。我通過快速Google搜索找到的最佳解決方案是Ryan Fait's Sticky Footer。 Ryan的教程通過將100%的高度放在包裝元素上(其中包含除了頁腳之外的內容)。這個100%的高度就是讓你的內容將頁腳推到窗口的底部,然後頁腳利用負邊距蠕動回到可見區域(這就是爲什麼你需要知道頁腳的高度......負頁邊距必須與頁腳元素高度相同)。還有一些額外的部分可以在所有瀏覽器中可靠地工作(如<div class="push"></div>),但基本上這是發生了什麼事情。

  • 較新的純CSS解決方案需要使用display: table;(IE7不支持),但允許使用可變高度頁腳(請參閱Sticky CSS footers: The flexible way)。這可能是我會推薦的方法。

第二篇文章的作者提到,Javascript可以用來添加IE7的支持,但是停止編寫實際的Javascript。在回答這個問題時,StatCounter列出IE7市場滲透率約爲1.28%。我會把它留給你來確定IE7的支持是否重要,但是如果我願意的話,我想補充0.02美元:-p首先,IE7的所有用戶都有升級到IE8的路徑,拒絕更新的用戶是隻會讓Web開發人員的生活變得更加困難(IE7到IE8爲許多重要的CSS2選擇器開闢了可能性,並修復了許多嘮叨的浮動問題,並使得display: table;成爲可能)。除了讓網絡開發者的生活更加艱難,他們還面臨着大量的瀏覽器黑客攻擊,這些攻擊會危及他們的計算機,使他們成爲黑客尋找擴展殭屍軍隊的一個容易的目標(這使得security more difficult for everyone else)。其次,Do websites need to look exactly the same in every browser?(當然,答案是「否」)只要在瀏覽器窗口的底部有一個頁腳可以被認爲是漸進式增強,那麼您就不必擔心了。

這就是說...我更新了代碼,所以它應該在IE7中工作:-請看看jsfiddle,讓我知道它是如何工作的。

+0

Ryan的教程不是簡單地將頁腳向下按一定量的像素嗎?我不認爲這很方便或強大。如果您不知道頁腳需要多少,可以使用第二個教程嗎? 我知道一些JavaScript,我願意學習更多(我一直這樣做)如果它意味着我的佈局需要它的方式。 – SudoSilman

+0

我已經更新了答案,並解釋了Ryan代碼的作用,並進一步解釋了我對'display:table;'方法的建議。我還添加了Javascript以使'display:table;'方法在不支持該顯示值的瀏覽器中工作。 – thirdender

+0

非常感謝!我一定會研究這一點,並試圖讓它工作! – SudoSilman

-1

使用CSS的位置是:固定

您可以參考此鏈接關於它的更多信息。 http://www.w3schools.com/cssref/pr_class_position.asp

+0

我感覺好像不會將這種佈局應用於多個頁面。如果我製作頁面的內容多於屏幕高度?我希望在內容塊之後將頁腳向下推。 – SudoSilman

+0

所以你希望頁腳不在屏幕的底部。如果是,那麼你可以使用position:absolute; bottom:0px; 。對於內容小於屏幕的黑色空間,您可以使用min-height – user1494855

+0

Sorta,當頁面內容長度超過頁面高度時,我希望它位於頁面的底部。如果頁面內容不佔用整個頁面,我希望內容框拉伸,以便頁腳整齊地放置在屏幕底部。 – SudoSilman

相關問題