更改 Bootstrap 中的行高

Ale*_*lex 4 html bootstrap-4

我有以下 html 正文:

<body>
    <div class="container-fluid h-100 border">
        <div class="row h-50">
            <div class="col-sm-12 bg-danger">Row1</div>
        </div>
        <div class="row h-50">
            <div class="col-sm-12 bg-primary">Row2</div>
        </div>
    </div>
</body>
Run Code Online (Sandbox Code Playgroud)

我想更改两行的高度,但总是让它们都填充容器。事实上,它们都占据了屏幕的一半。但是,如果我将高度更改为 20 和 80,这些行就会恢复为屏幕顶部的 2 个小行。唯一有效的其他高度是 100 和 100,它们只会形成 2 个全屏行,这也不是我想要的。

有任何想法吗??

mah*_*han 9

使用flex-grow-1

使用这些类div.container-fluid

  1. d-flex - 使其弯曲
  2. flex-column - 将其方向更改为列
  3. h-100 - 使其高度为 100%。

如果您希望两者均rows等地填充容器,请flex-grow-1分别使用它们。

body,
html {
  height: 100%;
}
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div class="container h-100 d-flex flex-column">
  <div class="row flex-grow-1 bg-danger"></div>
  <div class="row flex-grow-1 bg-primary"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

行填充容器的 50%


否则,使用flex-grow-1其中之一并控制第二个的高度。

body,
html {
  height: 100%;
}
Run Code Online (Sandbox Code Playgroud)
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css" integrity="sha384-ggOyR0iXCbMQv3Xipma34MD+dH/1fQ784/j6cY/iJTQUOhcWr7x9JvoRxT2MZw1T" crossorigin="anonymous">

<div class="container h-100 d-flex flex-column">
  <div class="row h-25 bg-danger"></div>
  <div class="row flex-grow-1 bg-primary"></div>
</div>
Run Code Online (Sandbox Code Playgroud)

第一行填充容器的 25%,第二行填充剩余的可用空间。


容器的所有父级都需要有100%高度。