回答
借助Flexbox,就可以做这
html, body {
margin: 0;
}
body {
display: flex; /* IE fix */
}
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
width: 100%; /* IE fix */
}
.top, .bottom {
flex: 1; /* fill remaining space */
background: lightblue;
}
.middle {
background: lightgreen;
}
<div class="container">
<div class="top">
</div>
<div class="middle">
Content
</div>
<div class="bottom">
</div>
</div>
如果你需要,你还可以添加一个min-height
到top
/bottom
元素
html, body {
margin: 0;
}
body {
display: flex; /* IE fix */
}
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
width: 100%; /* IE fix */
}
.top, .bottom {
flex: 1; /* fill remaining space */
background: lightblue;
min-height: 200px; /* set a min-height */
}
.middle {
background: lightgreen;
}
<div class="container">
<div class="top">
</div>
<div class="middle">
Content
</div>
<div class="bottom">
</div>
</div>
.top{
min-height:100px;
}
.bottom{
min-height:100px;
}
,或者你可以使用不同的meseurment - 像VH - 的观点高度等 忘了补充 - 它会进入你的CSS文件
但这不会填充剩余空间。 – KevinKelbie
你想使用jQuery吗?你需要使用动态计算,如果是的话,我可以告诉你简短的jQuery功能,可以帮助你 –
当然,如果这是最简单的方法。谢谢您的帮助! :) – KevinKelbie
这里是你较真表的解决方案。
html, body {
margin: 0;
height:100%;
}
.container {
display: table;
height: 100%;
width: 100%;
}
.container > div {
display: table-row;
}
.table-cell {
display: table-cell;
}
.top, .bottom {
background: #ADD8E6;
}
.middle {
background: #90EE90;
height:1px;
}
<div class="container">
<div class="top">
<div class="table-cell">
</div>
</div>
<div class="middle">
<div class="table-cell">
Content
</div>
</div>
<div class="bottom">
<div class="table-cell">
</div>
</div>
</div>
另一个伟大的解决方案谢谢!这比柔性解决方案更友好吗? – KevinKelbie
不使用flexbox的唯一原因是如果你想支持IE9。同样在具有复杂结构的大型网站上,flexbox可以稍微增加页面加载时间。我个人只有在没有其他方法来实现我想要的布局时才使用flexbox。所有人都说,在这种情况下,我看不到使用flex的任何问题。 – WizardCoder
- 1. 使内容div填充剩余高度
- 2. CSS填充剩余宽度
- 3. 输入元素填补剩余宽度
- 4. 设置div高度以填充剩余高度
- 5. Flexbox拉伸div高度以填充父div的剩余高度
- 6. 输入元素来填充其容器的剩余宽度
- 7. 填充包含元素的剩余宽度
- 8. 填充元素和剪切文本之间的剩余宽度
- 9. 让跨度填充剩余宽度?
- 10. 获取<canvas>元素以填充父div中的剩余高度?
- 11. 2 TextViews平均填充剩余空间
- 12. 制作格填充剩余宽度:内
- 13. 填写动态div的剩余高度
- 14. IE8剩余高度
- 15. div用绝对填充家长的剩余高度
- 16. Android GridView高度自动填充剩余空间
- 17. 儿童DIV用可滚动内容填充剩余高度
- 18. 用可滚动Div填充剩余高度
- 19. Angular 4 - Google地图高度填充剩余空间
- 20. Flex项目 - 填充剩余容器的高度
- 21. 用CSS填充剩余的屏幕高度
- 22. 有一个表格填充其容器的剩余高度
- 23. 填充剩余空间(CSS)
- 24. ImageView填充剩余空间
- 25. 如何在“display:table-row”元素中嵌入元素“display:table”以填充MSIE10中的剩余高度?
- 26. 用Javascript/Jquery(100%高度Web应用程序)填充剩余高度
- 27. 使用JS删除元素,但剩余空间填充
- 28. 填充剩下的高度相对DIV
- 29. CSS股利填充剩下的高度
- 30. DIV填充剩下的高度计算
伟大的回应,这似乎正是我需要感谢! – KevinKelbie
@WizardCoder我知道你没有说它是坏的,但再次看看,IE10支持2012年的Flexbox语法,它在上面的代码中是'display:-ms-flexbox;','-ms-flex-direction :列'和'-ms-flex:1;' – LGSon