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

gra*_*ury 67 html css

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

Jez*_*mas 78

您正在寻找的是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*/
}
Run Code Online (Sandbox Code Playgroud)
<div id="wrap">
  <div id="main"></div>
</div>

<div id="footer"></div>
Run Code Online (Sandbox Code Playgroud)

  • 我的页脚没有固定的高度...它可能在不同的设备上,或纵向和横向格式之间有所不同.怎么做? (8认同)
  • 由于某种原因,添加<!DOCTYPE html>会破坏页脚. (5认同)

Cha*_*ala 27

你可以position:fixed;用来bottom.

例如:

#footer{
 position:fixed;
 bottom:0;
 left:0;
}
Run Code Online (Sandbox Code Playgroud)

  • 然后,无论实际页面上有多少内容,它都会显示在屏幕底部.不好的建议. (58认同)
  • @mwieczorek呃,我想这取决于你想要什么?我不会说Chamara的建议很糟糕.它实际上是我想要的,它对我有用:)在屏幕调整大小时,我的页脚保持在我想要的位置:在我的页面底部.谢谢Chamara. (13认同)