javascript - 删除 div 之间的垂直空白,尤其是在遇到断点之后

标签 javascript jquery html css responsive-design

我目前正在设计一个响应式页面,我遇到了 div 之间的空白问题,尤其是在遇到断点之后。

body, html {
  padding: 0;
  margin: 0;
}
.header {
  padding-top: 5px;
  background-color: red;
  width: 100%;
}
.sub-header {
  padding: 5px;
  margin: 0px;
  background-color: yellow;
  width: 100%;
}
.main-content {
  padding: 5px;
  background-color: blue;
  width: 100%;
}
.footer {
  position: absolute;
  bottom: 0;
  padding: 5px;
  background-color: green;
  width: 100%;
}
@media screen and (max-width: 320px) {
  .sub-header {
    display: none;
  }
}
}
<div class="header">Header
  <div class="sub-header">Sub-Header</div>
</div>
<div class="main-content">Auto adjust size</div>
I want to have the blue div take up the remaining         space in this white space, as well as after the sub-header is removed at the break point.
<div class="footer">footer</div>

这是我正在经历的一个快速模型:http://jsfiddle.net/gaych7vp/6/

我明白我必须做些什么才能让它在遇到断点之前占据剩余的空白空间(我假设只是调整高度值),但我将如何制作蓝色 div占用断点后隐藏黄色 div 时创建的剩余空白区域?

我对 javascript 仍然很陌生,但从我读过的其他答案来看,可以通过创建一个函数来找到浏览器的高度,然后从其他 div 中减去它。这可能吗?如果可能,我该如何实现?

最佳答案

使用具有不同top值的position:absolute

.main-content {
    position:absolute;
    top:51px;
    bottom:0px;
}

@media screen and (max-width: 320px) {
    .main-content {
        top: 23px;
    }
}

fiddle

另一种方法是使用 display:tabledisplay:table-row

body, html{
    width:100%;
    height: 100%;
}
body{
    display:table;
}
.main-content {
    width: 100%;
    height: 100%;
    display:table-row;
}

fiddle

Make a div fill the height of the remaining screen space

关于javascript - 删除 div 之间的垂直空白,尤其是在遇到断点之后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30282968/

相关文章:

javascript - JQuery:如何在源更改之前隐藏图像

javascript - 简单的javascript动画

javascript - 单击按钮时重新加载 Div

jquery - 如何在选择器搜索输入中添加占位符

javascript - 获取 float 在下一行的第一个元素

css - 使 div 可聚焦并在选择上应用父链接

javascript - 如何使用javascript打开firefox插件?

javascript - Firebase 数据未在 HighCharts 中绘制

javascript - 移除类中定义的 EventListener

javascript - 页面在 chrome 和 FF 中不重定向,但在 IE 中重定向