如何避免Flex容器高度塌陷?
2025-04-15
16
参考资料
设置固定高度
.flex-container { display: flex; height: 300px; /* 固定高度 */ }
使用
min-height
.flex-container { display: flex; min-height: 200px; /* 最小高度 */ }
设置
align-items
属性.flex-container { display: flex; align-items: stretch; /* 默认值,子项拉伸填充高度 */ }
子项设置高度
.flex-item { height: 100%; /* 子项继承容器高度 */ }
使用
flex-grow
.flex-item { flex-grow: 1; /* 子项扩展填充剩余空间 */ }
清除浮动(如果存在浮动元素)
.flex-container::after { content: ""; display: table; clear: both; }
示例:
<div class="flex-container" style="display: flex; min-height: 200px;"> <div class="flex-item" style="flex-grow: 1; background: lightblue;"></div> </div>
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。