简体   繁体   English

flexbox 和引导卡的填充高度

[英]Filling height with flexbox and bootstrap cards

I have a page using Bootstrap containers and cards.我有一个使用 Bootstrap 容器和卡片的页面。 I am trying to get .card-body to fill the full height of the screen but the height ends after the end of the content.我试图让.card-body填满屏幕的整个高度,但高度在内容结束后结束。 Here is a fiddle demonstrating the problem.这是一个演示问题的小提琴。 Fiddle小提琴

 .page-container { min-height: 100vh; }.main-content { min-height: calc(100vh - 0px); -webkit-box-flex: 1; flex-grow: 1; }.container { width: 100%; }.card { display: flex; flex-direction: column; }.card-body { -webkit-box-flex: 1; flex: 1 1 auto; background: #ccc; }
 <div class="page-container"> <div class="main-content"> <div class="container form-container"> <div class="card"> <div class="card-body"> This should be full height! </div> </div> </div> </div> </div>

All containers under .main-content have no heights defined. .main-content下的所有容器都没有定义高度。 So why would .card-body be full height?那么为什么.card-body会是全高呢? (Note that in HTML/CSS, most elements are height: auto by default. ) (请注意,在 HTML/CSS 中,大多数元素默认为height: auto

You could either add heights to the nested containers, or add display: flex , which will give the children full height through the align-items: stretch default .您可以为嵌套容器添加高度,也可以添加display: flex ,这将通过align-items: stretch default 为子级提供完整高度

 .page-container { min-height: 100vh; }.main-content { min-height: calc(100vh - 0px); flex-grow: 1; display: flex; /* new */ }.container { flex: 1; /* new; for width; */ display: flex; /* new */ }.card { flex: 1; /* new */ display: flex; flex-direction: column; }.card-body { flex: 1 1 auto; background: #ccc; } body { margin: 0; }
 <div class="page-container"> <div class="main-content"> <div class="container form-container"> <div class="card"> <div class="card-body"> <b>This is now full height!</b> </div> </div> </div> </div> </div>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM