Bootstrap 4行填充剩余高度

56

我正在努力让一行充满剩余的可用高度。我尝试在行类中添加h-100,但这会在屏幕底部留下空白。肯定有方法可以实现,但我完全被卡住了。以下是我的代码:

<div class="container-fluid h-100">
  <div class="row justify-content-center h-100">
    <div class="col-4 bg-red">
      <div class="h-100">
        <div class="row justify-content-center bg-purple">
          <div class="text-white">
            <div style="height:200px">ROW 1</div>
          </div>
        </div>
        <div class="row justify-content-center bg-blue">
          <div class="text-white">ROW 2</div>
        </div>
      </div>
    </div>
    <div class="col-8 bg-gray"></div>
  </div>
</div>

CodePen: https://codepen.io/ee92/pen/zjpjXW/?editors=1100

enter image description here

我想让蓝色行(第二行)填满所有红色空间。有什么建议吗?

谢谢。

3个回答

78

使用Bootstrap 4.1的flex-grow-1类...

https://codeply.com/go/Iyjsd8djnz

html,body{height:100%;}

.bg-purple {
  background: rgb(48,0,50);
}
.bg-gray {
  background: rgb(74,74,74);
}
.bg-blue {
  background: rgb(50,101,196);
}
.bg-red {
  background: rgb(196,50,53);
}
<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-fluid h-100">
  <div class="row justify-content-center h-100">
    <div class="col-4 bg-red">
      <div class="h-100 d-flex flex-column">
        <div class="row justify-content-center bg-purple">
          <div class="text-white">
            <div style="height:150px">ROW 1 - fixed height</div>
          </div>
        </div>
        <div class="row justify-content-center bg-blue flex-grow-1">
          <div class="text-white">ROW 2 - grow remaining height</div>
        </div>
      </div>
    </div>
    <div class="col-8 bg-gray"></div>
  </div>
</div>

更新4.3.1: 另一个使用vh-100实用类的示例

https://codeply.com/go/h3bZbM6eSS

.bg-purple {
  background: rgb(48,0,50);
}
.bg-gray {
  background: rgb(74,74,74);
}
.bg-blue {
  background: rgb(50,101,196);
}
.bg-red {
  background: rgb(196,50,53);
}
<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-fluid">
  <div class="row justify-content-center min-vh-100">
    <div class="col-4 bg-red">
      <div class="d-flex flex-column h-100">
        <div class="row justify-content-center bg-purple">
          <div class="text-white">
            <div style="height:150px">ROW 1 - fixed height</div>
          </div>
        </div>
        <div class="row justify-content-center bg-blue flex-grow-1">
          <div class="text-white">ROW 2 - grow remaining height</div>
        </div>
      </div>
    </div>
    <div class="col-8 bg-gray"></div>
  </div>
</div>


相关链接:如何使行占据剩余的高度


一切都很好,直到第一行的文本开始增长并隐藏在第二行下面... https://codepen.io/anon/pen/maVoKy :/ - m1l05z
@Nate,你看了我附上的示例了吗?无论如何,在我的情况下,设置固定高度都不是一个选项。 - m1l05z
你使用的是哪个浏览器?在 Chrome 中,对我来说,你的第一行超出了屏幕的高度,但它并没有“隐藏”在第二行下面——相反,它将第二行推到了屏幕外。在 Safari 中,它会隐藏在第二行下面,这感觉比 Chrome 做的更合适。你究竟想要实现什么?我也不想要一个固定的高度,但我知道它足够小,可以适应我的预期最小高度。我认为在这个问题领域中,你有一行你知道是“小”的,然后另一行应该扩展到剩余的空间。这不是你想要的吗? - Nate
@Nate,由于此答案中“ROW 1”具有固定高度,因此它不应该增长,但内容应该向右或向下溢出。 - C4d
@C4d 我明白了。在我的代码中,我移除了第一行的固定高度,因为我想让两行占据整个空间,但第一行的高度最小。听起来 m1l05z 也想要同样的效果。也许这不是解决我的问题的最佳方法,但这是我找到的方法 =)。 - Nate
如果第二行显示了一个很长的列表,并且希望它保持剩余高度,而不是延伸到列表的末尾,该怎么办? - 7skies

7
这是一个解决方案。包装器div必须具有h-100,适应高度的div必须具有flex-grow-1和overflow-auto。这样,当其内容小于可用空间时,div将增长以填充空间,并在内容高于可用空间时显示滚动条。 Demo jsfiddle
<div class="h-100 d-flex flex-column bg-yellow px-2">
<div class="flex-column justify-content-center bg-purple px-2">        
    <div class="text-white p-0" style="height:50px">HEADER</div>
</div>

<div class="flex-column justify-content-center bg-red text-white px-2 flex-grow-1 overflow-auto">

        <div>Item1</div>
        <div>Item2</div>
        INNER text 1<br>
        INNER text 2<br>

</div>

<div class="flex-column justify-content-center bg-darkblue px-2">
    <div class="text-white p-0" style="height:50px">FOOTER</div>
</div>                


非常感谢您的回答。这解决了我的问题。您能否也告诉我如何使用两列实现相同的效果?我想要有ColumnA和ColumnB,两者都应该适应高度并独立滚动。 - Haider

1

如果有人已经厌倦了让这个工作,这里有一个超级简单的解决方案,适用于大多数情况。只需将以下类添加到您要填充剩余高度的 div 中: 请注意,100vh 是可见高度的 100%,而 200px 是上面其余 div 的总固定高度。

.fillRemaining {
height: calc(100vh - 200px);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接