如何在 Bootstrap 中自动调整行高?

iDe*_*Dec 5 html css twitter-bootstrap bootstrap-4

我正在学习 Bootstrap,我试图弄清楚如何在这种情况下自动调整行高:

  • 容器流体中有 3 行
  • row1必须调整到其内容的高度
  • row3必须调整到其内容的高度并位于视口的底部
  • row2应将其高度调整为row1row3之间的空间以填充容器流体

html, body, .container-fluid {
  height: 100%;
  background: lightyellow;
}

.col {
  border: solid 1px #6c757d;
}

.content-1 {
  height: 50px;
  background-color: lightcoral;
}

.content-2 {
  height: 200px;
    background-color: lightgreen;
}

.content-3 {
  height: 25px;
  background-color: lightblue;
}
Run Code Online (Sandbox Code Playgroud)
<div class="container-fluid">
  <div class="row">
    <div class="col">
      <div class="content-1">
      ROW 1 -> Height of its content
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col">
      <div class="content-2">
      ROW 2 -> Height between row1 and row3 automatically adjusted
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col">
      <div class="content-3">
      ROW 3 -> Height of its content
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

JSFiddle 演示

这样做的最佳方法是什么?先感谢您!

Zim*_*Zim 8

您正在寻找 flexbox 实用程序类。d-flex用于display:flex,flex-grow-1并将使第二个子 div 填充剩余的高度。用flex-column你想要的垂直布局。

<div class="container-fluid d-flex flex-column">
  <div class="row">
    <div class="col">
      <div class="content-1">
      ROW 1 -> Height of its content
      </div>
    </div>
  </div>
  <div class="row flex-fill">
    <div class="col d-flex flex-column">
      <div class="content-2 h-100">
      ROW 2 -> Height between row1 and row3 automatically adjusted
      </div>
    </div>
  </div>
  <div class="row">
    <div class="col">
      <div class="content-3">
      ROW 3 -> Height of its content
      </div>
    </div>
  </div>
</div>
Run Code Online (Sandbox Code Playgroud)

https://www.codeply.com/go/IIVJqGJ2qG

注意:在自定义 CSS 中设置的固定高度已被删除,以允许页眉/页脚为其内容的高度,而内容行填充剩余的高度。


相关:
Bootstrap 4:如何使行拉伸剩余高度?
Bootstrap - 在页眉和页脚之间填充流体容器