【问题标题】:How to let short content pages reach the bottom of the browser window and then a footer should appear如何让短内容页面到达浏览器窗口底部,然后出现页脚
【发布时间】:2011-02-07 22:41:59
【问题描述】:

在这种情况下,我为这个 Web 应用程序开发了一个 CSS 代码。有时生成的数据太小,网站的页脚出现在页面中间,看起来很奇怪。

我想将背景的空白推到浏览器的底部,然后是页脚。如果页面很长,则该文本不会被页脚重叠。

有人可以在这里帮我处理这段代码吗?

我一直在尝试使用我在此页面上找到的一些代码:http://matthewjamestaylor.com/blog/keeping-footers-at-the-bottom-of-the-page 谈到了几乎相同的问题,但我无法完全完成:
什么是我做错了吗?

@charset "utf-8";
body {
    margin: 0;
    padding: 0;
    text-align: center;
    height:100%;
    position: relative;
    height:100%; /* needed for container min-height */
    }
.spacer {
    clear: both;
    height: 0;
    margin: 0;
    padding: 0;
    font-size: 0.1em;
}
.spacer_left {
    clear: left;
    margin: 0 0 10px 0;
    padding: 0;
    font-size: 0.1em;
}
hr {
    height: 1px;
    margin: 20px 0 20px 0;
    border: 0;
    color: #ccc;
    background: #ccc;
}
#container {
    position:relative; /* needed for footer positioning*/
    height:auto !important; /* real browsers */
    height:100%; /* IE6: treaded as min-height*/
    min-height:100%; /* real browsers */

    width: 1160px;  /* width of the site ! */
    margin: 0 auto;
    padding: 0;
    border: 1px solid #333;
    text-align: left;
}
/* Context Bar */ 
h1#contexto {
    background:url('../images/menubarbg2.png');
    width:1160px;
    height:30px;
    position:relative;
    margin-top:10px;
    visibility: inherit;
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
}
#header {
    margin: 0;
    padding: 5px;
    height:70px;
}
h1#titulo {
    margin: 0;
    padding: 0 0 0;

}
#content {

 margin: -15px 20px 0 20px;
 /*padding: -6px 5px 20px 5px;*/
 padding:1em 1em 5em; /* bottom padding for footer */

}
div#content.columns {
    margin-left: 100px;
}
#content abbr, #content acronym {
    cursor: help;
    border-bottom: 1px dotted;
}
#content ul {
    list-style-type: square; 
}
#content ul li, #content ol li {
    margin: 0 0 0.4em 0;
    padding: 0; 
}
#content blockquote {
    width: 75%;
    margin: 0 auto;
    padding: 20px;
}

#footer  
{
 margin: 0;
 height: -30px;
 padding: 5px;
 clear: both;
 bottom:0; 
 position:relative;
}

更新:解决方案

@charset "utf-8";
body, html {
    margin: 0;
    padding: 0;
    text-align: center;
    position: relative;
    height:100%; /* needed for footer positioning*/
}
.spacer {
    clear: both;
    height: 0;
    margin: 0;
    padding: 0;
    font-size: 0.1em;
}
.spacer_left {
    clear: left;
    margin: 0 0 10px 0;
    padding: 0;
    font-size: 0.1em;
}
hr {
    height: 1px;
    margin: 20px 0 20px 0;
    border: 0;
    color: #ccc;
    background: #ccc;
}
#container {
    position:relative; /* needed for footer positioning*/
    min-height: 100%;/* needed for footer positioning*/
    height: auto !important;/* needed for footer positioning*/
    height: 100%;/* needed for footer positioning*/
    margin: 0 auto -30px;/* needed for footer positioning*/
    width: 1160px;
    padding: 0;
    border: 1px solid #333;
    text-align: left;
}
#header {
    margin: 0;
    padding: 5px;
    height:70px;
}
h1#titulo {
    margin: 0;
    padding: 0 0 0;
}
h1#contexto {
    background:url('../images/menubarbg2.png');
    width:1160px;
    height:30px;
    position:relative;
    margin-top:10px;
    visibility: inherit;
    font-family:Arial, Helvetica, sans-serif;
    font-size:12px;
}
#content {
    margin: -15px 20px 30px 20px; /* needed for footer positioning*/
}
div#content.columns {
    margin-left: 100px;
}
#content abbr, #content acronym {
    cursor: help;
    border-bottom: 1px dotted;
}
#content ul {
    list-style-type: square;
}
#content ul li, #content ol li {
    margin: 0 0 0.4em 0;
    padding: 0;
}
#content blockquote {
    width: 75%;
    margin: 0 auto;
    padding: 20px;
}
#footer, .push /* needed for footer positioning*/ {
    padding: 5px;
    clear: both;
    position:absolute;/* needed for footer positioning*/
    bottom:0;/* needed for footer positioning*/
    height: -30px;/* needed for footer positioning*/
    width:1150px;
}

【问题讨论】:

  • 请查看这篇文章的更新。代码的更正已经完成并且可以正常工作,所以我在这里发布它,以防万一有人想看到旧 css 和修复之间的区别。感谢用户 Yongho 和 Ricebowl 的帮助。

标签: html css layout footer


【解决方案1】:

您可以通过使正文高度等于浏览器窗口的高度(即:将高度设置为 100%),然后强制页脚位于浏览器窗口的任何高度的底部来获得“粘性页脚”是。

CSS:

body {
    height: 100%;
    position: relative;
}

#footer {
 position:absolute;
 bottom:0;
 height:60px;
 background:#ccc;
}

HTML:

<html><body>

 text here text here

 <div id="footer">
  Im in the footer and bottom of the page!
 </div>

</body></html>

【讨论】:

  • 好吧,我不知道怎么做。但是我只是根据您的建议对我的代码进行了一些编辑,并且它起作用了。这似乎是我以前做的同样的事情,但由于某种原因没有奏效。感谢您的热心帮助。非常感谢。
  • 我已经有一段时间没有尝试过这种方法了,但是从记忆中 - 这可能是一个文档类型问题 - 我似乎记得这会导致页脚始终在屏幕上,但是也可以在滚动时重叠溢出浏览器下边框的内容。虽然我没有投反对票,因为我记不清了。
  • 我猜你可以解决身体上 padding-bottom 的重叠问题。但是你希望页脚被推到内容的底部,滚动滚动......我不知道如何做到这一点,但也许使用 min-height 而不是 height。
  • 此特定案例的解决方案已添加到问题的内容中。感谢您的帮助!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-06-02
  • 2012-08-20
  • 2012-11-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多