覆盖可用高度,不带滚动条,用于外部和内部div

时间:2017-05-02 21:05:35

标签: html css

我创建了一个显示内容的jsFiddle



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

.body {
   position: relative;
   height: 100%;
   width: 100%;
   margin: 0 auto;
   background-color: #F7F4F2;
   text-align: center;
}

.form {
  background-color: #fff;
  padding: 50px 20px;
  color: #333;
}

.footer {
  margin-top: 26px;
  font-size: 20px;
  line-height: 26px;
}

.content {
  padding: 5% 5%;
}

<div class="container">
  <div class="body">
    <div class="content">
      <div class="form">
        <h1>Content</h1>
      </div>
      <div class="footer">
        <a href="#">This is a link</a>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

我未能做的是让container元素填充可用的视口,并使内部白色div也延伸到底部,并且填充符号受到尊重。

我还想将链接移动到视口的底部。

没有JavaScript,这可能吗?

2 个答案:

答案 0 :(得分:1)

使用flexbox这样的事情怎么样?

display: flex;放在.content上 然后在主子div上使用flex: 1;,然后使用height: 100vh;

现在你不需要100%宽度和高度的样式。

还使用了box-sizing:border-box,因此padding不会弄乱容器的大小。

&#13;
&#13;
* {
  box-sizing: border-box;
}

.container {
  margin: 0;
}

.body {
  position: relative;
  margin: 0 auto;
  background-color: #F7F4F2;
  text-align: center;
}

.content {
  display: flex;
  flex-direction: column;
  height: 100vh;
  padding: 5% 5%;
}
.form {
  flex: 1;
  background-color: #fff;
  padding: 50px 20px;
  color: #333;
}

.footer {
  margin-top: 26px;
  font-size: 20px;
  line-height: 26px;
}
&#13;
<div class="container">
  <div class="body">
    <div class="content">
      <div class="form">
        <h1>Content</h1>
      </div>
      <div class="footer">
        <a href="#">This is a link</a>
      </div>
    </div>
  </div>
</div>
&#13;
&#13;
&#13;

答案 1 :(得分:0)

使用此CSS:

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

每位家长需要height: 100%;,一直到html&amp;身体。如果没有它,那么动态的高度将无法发挥作用。

JSfiddle

相关问题