如何在页面底部固定页脚

rod*_*ves 12 css footer

在我的HTML中,我有一个div级别的"页脚".我希望它有一个bg到#000并占据整页宽度并且在它之后没有留下任何空格.

我目前正在使用这个CSS:

.footer {
  color: #fff;
  clear: both;
  margin: 0em 0em 0em 0em;
  padding: 0.75em 0.75em;
  background: #000;
  position: relative;
  top: 490px;
  border-top: 1px solid #000;
}
Run Code Online (Sandbox Code Playgroud)

但是这个css代码没有填充整页宽度.

有帮助吗?谢谢!

Chr*_*ski 21

我使用粘性页脚:http://ryanfait.com/sticky-footer/

/*

    Sticky Footer by Ryan Fait
    http://ryanfait.com/

    */

* {
  margin: 0;
}

html,
body {
  height: 100%;
}

.wrapper {
  min-height: 100%;
  height: auto !important;
  height: 100%;
  margin: 0 auto -142px;
  /* the bottom margin is the negative value of the footer's height */
}

.footer,
.push {
  height: 142px;
  /* .push must be the same height as .footer */
}
Run Code Online (Sandbox Code Playgroud)
<div class='wrapper'>
  body goes here
  <div class='push'></div>
</div>
<div class='footer'>Footer!</div>
Run Code Online (Sandbox Code Playgroud)

基本上,包装器的高度为100%,页脚的高度为负边距,确保页脚始终位于底部而不会导致滚动.

这应该实现你的目标,即拥有100%宽度的页脚和更窄的主体,因为div是块级元素,并且它们的宽度默认为其父级的100%.请记住,这里的页脚不包含在包装div中.

  • 如果页面内容比页面长,如果页脚与内容重叠怎么办? (4认同)