twitter-bootstrap 为什么 h-100 不起作用?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/50057619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-22 01:21:04  来源:igfitidea点击:

Why h-100 does not work?

htmltwitter-bootstrap

提问by zero_coding

I have the following layout:

我有以下布局:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  <link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
  <title>Bootstrap</title>
</head>
<body>

<div class="container bg-danger h-100">
  <div class="bg-warning border p-1">Exotic</div>
  <div class="bg-warning border p-1">Grooming</div>
  <div class="bg-warning border p-1">Health</div>
  <div class="bg-warning border p-1">Nutrition</div>
  <div class="bg-warning border p-1">Pests</div>
  <div class="bg-warning border p-1">Vaccinations</div>
</div><!-- container -->

<script src="https://code.jquery.com/jquery-3.3.1.slim.min.js" integrity="sha384-q8i/X+965DzO0rT7abK41JStQIAqVgRVzpbzo5smXKp4YfRvH+8abtTE1Pi6jizo" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.14.0/umd/popper.min.js" integrity="sha384-cs/chFZiN24E4KMATLdqdvsezGxaGsi4hLGOzlXwp5UZB1LY//20VyM2taTB4QvJ" crossorigin="anonymous"></script>
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/js/bootstrap.min.js" integrity="sha384-uefMccjFJAIv6A+rW+L4AHf99KvxDjWSu1z9VI8SKNVmz4sk7buKt/6v9KI65qnm" crossorigin="anonymous"></script>
</body>
</html>

The question is, why the divcontainer is not 100 percent height?

问题是,为什么div容器不是 100% 的高度?

Thanks

谢谢

回答by vicnoob

As my knowledge, height 100% is the div has the height equal to its parent, but here there is no parent for it. If you want to make the div's height equal to your window height, I tried wrap it with a div height = 100vh. https://jsfiddle.net/goLfLykw/

据我所知,高度 100% 是 div 的高度等于其父级,但这里没有父级。如果你想让 div 的高度等于你的窗口高度,我试着用 div height = 100vh 包裹它。https://jsfiddle.net/goLfLykw/

    <div style="height: 100vh">
      <div class="container bg-danger h-100">
        <div class="bg-warning border p-1">Exotic</div>
        <div class="bg-warning border p-1">Grooming</div>
        <div class="bg-warning border p-1">Health</div>
        <div class="bg-warning border p-1">Nutrition</div>
        <div class="bg-warning border p-1">Pests</div>
        <div class="bg-warning border p-1">Vaccinations</div>
      </div>
      <!-- container -->
    </div>

回答by Tomá? Hübelbauer

You should be able to set HTML and BODY heights to 100 % so that the divs parent is full of viewport height, then the div should stretch there. This is easy to achieve with flexbox:

您应该能够将 HTML 和 BODY 高度设置为 100%,以便divs 父级充满视口高度,然后 div 应该在那里伸展。这很容易用 flexbox 实现:

html { height: 100%; }
body { height: 100%; display: flex; }
body > div { flex: 1; }

This should stretch the div across the entire viewport.

这应该在整个视口中拉伸 div。