html - Flexbox - Fill remaining space
问题描述
I have a basic flexbox layout like this..
body,html {
height:100%;
width:100%;
}
#container {
width:100%;
background:grey;
display:flex;
flex-direction:column;
}
.top {
background:red;
flex:1;
padding:20px;
}
.bottom {
background:yellow;
flex:1;
padding:20px;
}
<div id="container">
<div class="top">
Top Content
</div>
<div class="bottom">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut venenatis, arcu vitae sollicitudin pellentesque, quam libero imperdiet urna, eu rhoncus lorem dolor sed neque. Donec ex risus, pretium ut maximus fringilla, euismod id mi. Phasellus ligula sem, iaculis ut urna eget, efficitur vulputate sem.
</div>
</div>
I am trying to make the top div fill the remaining space, the bottom div is dynamic so the height changes depending on the text. My result looks like this..
Is flexbox the best thing to use in this situation?
解决方案
在这种情况下使用 flexbox 是不是最好的选择?
是的,在这种情况下使用 flexbox 是个好主意。
据我了解,要达到您想要的结果,您需要设置height: 100%
on#container
以便它占据所有可用空间。然后设置flex-grow: 0
,.bottom
使其不会增长。
所以你的CSS应该是:
#container {
width:100%;
height:100%;
background:grey;
display:flex;
flex-direction:column;
}
.top {
background:red;
flex: 1 0 auto; // Grow, don't shrink
padding:20px;
}
.bottom {
background:yellow;
flex: 0 0 auto; // don't grow, take up only needed space
padding:20px;
}
推荐阅读
- c++ - 是否可以使用类似于 python 的请求(或其他 C++ 库)的 curl 库进行 API 流式传输?
- netbeans - 为什么我在 NetBeans 安装中出现错误
- javascript - JavaScript onclick 事件从动态生成的 html 元素中获取错误的 id
- php - 我的 PHP 网站上的未定义索引错误
- c++ - 移动 VS 项目文件时关闭时出现 WSANOTINITIALISED (10093) 错误
- android - Android中ffplay的任何解决方案?
- python - 无法实现依赖下拉列表
- angular - Angular 8 路由重写失败
- jquery - 仅在特定区域拖放文件?
- maximo - 通过自动化脚本访问 DOM 元素