2012-08-25 625 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

相关问题