如何将页脚保持在屏幕底部

时间:2013-09-11 11:29:49

标签: html css

设置网页的最佳做法是,如果该网页上显示的内容/文字非常少,则页脚会显示在浏览器窗口的底部而不是网页的一半?

6 个答案:

答案 0 :(得分:72)

您正在寻找的是 CSS Sticky Footer

* {
  margin: 0;
  padding: 0;
}

html,
body {
  height: 100%;
}

#wrap {
  min-height: 100%;
}

#main {
  overflow: auto;
  padding-bottom: 180px;
  /* must be same height as the footer */
}

#footer {
  position: relative;
  margin-top: -180px;
  /* negative value of footer height */
  height: 180px;
  clear: both;
  background-color: red;
}


/* Opera Fix thanks to Maleika (Kohoutec) */

body:before {
  content: "";
  height: 100%;
  float: left;
  width: 0;
  margin-top: -32767px;
  /* thank you Erik J - negate effect of float*/
}
<div id="wrap">
  <div id="main"></div>
</div>

<div id="footer"></div>

答案 1 :(得分:25)

您可以position:fixed;使用bottom

例如:

#footer{
 position:fixed;
 bottom:0;
 left:0;
}

答案 2 :(得分:2)

<强> HTML

<div id="footer"></div>

<强> CSS

#footer {
   position:absolute;
   bottom:0;
   width:100%;
   height:100px;   
   background:blue;//optional
}

答案 3 :(得分:0)

设置其位置:fixed和bottom:0,以便它始终位于浏览器窗口的底部

答案 4 :(得分:-1)

也许最简单的方法是使用position: absolute来修复底部,然后使用合适的边距/填充来确保其他文本不会溢出到顶部。

<强>的CSS:

<style>
  body {
    margin: 0 0 20px;
  }
  .footer {
    position: absolute;
    bottom: 0;
    height: 20px;
    background: #f0f0f0;
    width: 100%;
  }
</style>

这是html的主要内容。

<div class="footer"> Here is the footer. </div>

答案 5 :(得分:-3)

使用这种风格

min-height:250px;
height:auto;
相关问题