html - 覆盖外部和内部div无滚动条的可用高度

标签 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>

我没有做的是让 container 元素填充可用的视口(viewport),并让内部的白色 div 也拉伸(stretch)到底部并考虑填充。

我还想将链接移动到视口(viewport)底部。

如果没有 JavaScript,这可能吗?

最佳答案

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

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

现在您不需要所有宽度和高度为 100% 的样式。

还使用了 box-sizing: border-box 这样填充就不会影响容器的大小。

* {
  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;
}
<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>

关于html - 覆盖外部和内部div无滚动条的可用高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43747223/

相关文章:

html - 在同一个 div 中以不同方式对齐图像和文本

php - 我无法执行此查询... 'mysql' 'phpmyadmin'

jquery - 如何将 css 背景色仅应用于数据表单元格中的文本

css - 为什么是 em 而不是 px?

css - 我可以在我的网站中使用两个 Wordpress Logo - 一个用于响应式设计,另一个用于非响应式设计吗?

javascript - parent 找到或 sibling 或下一个都行不通

javascript - 使用 JS Canvas 绘制对象数组

jquery - 如何使用 jquery 在输入类型 ="text"和 textarea 之间切换

jQuery Mobile 无法正确调整页面大小

javascript - 使用 javascript 滚动查看 firefox