如何防止内容在页脚下?

时间:2018-03-11 23:47:22

标签: html css html5 css3

我在我的容器div上使用min-height: 200vwposition: relative,我使用position: absolutebottom: 0在其中有一个页脚。如何防止内容在页脚下方,并使页脚向下推?



.container {
  position: relative;
  min-height: 200vh;
  width: 100%;
 }
 
 .content-text {
  font-size: 2em;
 }
 
 footer {
  width: 100%;
  height: 10em;
  position: absolute;
  bottom: 0;
  background: red;
  opacity: .5;
}

<div class="container">

  <p class="content-text">Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main  </p>

  <footer>
    <p>Footer Content</p>
  </footer>
</div>
&#13;
&#13;
&#13;

1 个答案:

答案 0 :(得分:3)

根据您的代码,一种简单的方法是为容器提供与padding-bottom的高度等效的footer

&#13;
&#13;
.container {
  position: relative;
  min-height: 200vh;
  width: 100%;
  /* added */
  padding-bottom: 10em;
}

.content-text {
  font-size: 2em;
}

footer {
  width: 100%;
  height: 10em;
  position: absolute;
  bottom: 0;
  background: red;
  opacity: .5;
}
&#13;
<div class="container">

  <p class="content-text">Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content...
    Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content...
    Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content...
    Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content...
    Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content...
    Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content...
    Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content... Main content...
    Main content... Main content... Main content... Main content... Main content... Main content... Main </p>

  <footer>
    <p>Footer Content</p>
  </footer>
</div>
&#13;
&#13;
&#13;