如何让div粘在屏幕底部并让中间内容滚动?

时间:2017-12-04 08:16:42

标签: html css

我正在尝试使用粘性页脚,其中页脚始终可见并停靠在底部,而中间内容将在内容添加到其中时滚动。我正在使用这种方法来实现:

/* Reset */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}


/* Essentials */

.container {
  display: table;
}

.content {
  display: table-row;
  height: 100%;
}

.content-body {
  display: table-cell;
}

.scroller {
  max-height: 100%;
  overflow-y: auto;
}


/* Aestetics */

.container {
  width: 100%;
  height: 100%;
}

.header,
.footer {
  padding: 10px 20px;
  background: #f7f7f7;
}

.scroller {
  padding: 20px;
  background: #e7e7e7;
}

p {
  margin-top: 0;
  margin-bottom: 1rem;
}

p:last-child {
  margin-bottom: 0;
}
<div class="container">
  <header class="header">
    <p>This is the header</p>
  </header>
  <section class="content">
    <div class="content-body">
      <div class="scroller">
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
      </div>
    </div>
  </section>
  <footer class="footer">
    <p>This is the footer.</p>
  </footer>
</div>

这适用于Chrome和FF,但在IE中不起作用。在IE中是否有一个简单的解决方案,或者有人知道更好的解决方案吗?

3 个答案:

答案 0 :(得分:2)

您可以简单地考虑这样的固定位置:

/* Reset */

html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
}


/* Essentials */


.content {
  height: 100%;
}

.scroller {
  max-height: 100%;
  overflow-y: auto;
}


/* Aestetics */

.container {
  width: 100%;
  height: 100%;
}

.header,
.footer {
  position:fixed;
  right:0;
  left:0;
  padding:20px;
  background:#fff;
  
}

.footer {
 bottom:0;
}
.header {
 top:0;
}

.scroller {
  padding: 20px;
  background: #e7e7e7;
  margin:40px auto;
}

p {
  margin-top: 0;
  margin-bottom: 1rem;
}

p:last-child {
  margin-bottom: 0;
}
<div class="container">
  <header class="header">
    <p>This is the header</p>
  </header>
  <section class="content">
    <div class="content-body">
      <div class="scroller">
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
        <p>This is the content.</p>
      </div>
    </div>
  </section>
  <footer class="footer">
    <p>This is the footer.</p>
  </footer>
</div>

答案 1 :(得分:0)

您可以添加固定位置以在IE中解决此问题:

.header {
  position: fixed; left: 0; top: 0; width: 100%
}

检查updated fiddle

答案 2 :(得分:0)

.footer {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
}