如何设置 flexbox 容器的高度以便它使用“剩余空间”

Tod*_*nor 5 css flexbox

我正在努力理解 flexbox 容器如何与其他块交互。我的页面上只有一个 flexbox,我可以做我想做的事。但是当我混入其他页面元素时,事情变得很奇怪。问题似乎是空间分配。

我的 flexbox 容器似乎需要一个明确的高度。如果我不指定,我不会得到包装行为。我不确定如何指定 flexbox 容器的高度。

如果我将 flexbox 容器的高度设置为 100%,它会根据需要包装,但我被滚动条卡住了:我分配了超过 100% 的高度。我想在 flexbox 容器上方分配 100px。所以我需要将 flexbox 容器的高度设置为 100% - 100px。我不能混合绝对和相对测量。往下看,我宁愿根本不必做这个数学运算,这将成为维护方面的麻烦。

下面是一个例子,说明我哪里出错了。我想在页面顶部有一些说明。说明应跨越页面的宽度。在下面的空间中,我想要一组按钮。按钮应根据需要换行以使用多列。

我可以通过按钮 flexbox 容器内的说明使其工作。但是,它不会像我想要的那样 100%,它会与我的按钮混在一起。

<html>
    <head>
        <style>
            *  {
                margin: 0;  
                padding: 0;
                border: 1px solid #A00;
            }
            .instructions {
                height: 100px;
                background: linear-gradient(to bottom, #ffffff 0%, #999 100%);  
            }
            .container {
                display: flex;
                flex-direction:  column;
                flex-wrap: wrap;  
                height: 80%;        
            }
            .button {
                width: 200px;
                height: 50px;
                margin: 10px;
                background: linear-gradient(to bottom, #ffffff 0%, #BBB 100%);          
                border: 1px solid #CCC;
                text-align: center;
            }
        </style>
    </head>
    <body>
        <div class="instructions">Instructions  go here.</div>
        <div class="container">            
            <div class="button">This is Button 1</div>
            <div class="button">Thar be Button 2</div>
            <div class="button">Yarr, button 3</div>
            <div class="button">Hey that is a nice looking button.</div>
            <div class="button">Click Me!</div>
        </div>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

Mic*_*ker 6

你必须给这个部分一个高度来限制它使按钮环绕,否则 flex 元素只会增长以适应它里面任何东西的高度。

我会height: calc(100vh - 100px)在 flex 容器上使用它来占用所有可用空间。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

.instructions {
  height: 100px;
  background: linear-gradient(to bottom, #ffffff 0%, #999 100%);
}

.container {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  height: calc(100vh - 100px);
}

.button {
  width: 200px;
  height: 50px;
  margin: 10px;
  background: linear-gradient(to bottom, #ffffff 0%, #BBB 100%);
  border: 1px solid #CCC;
  text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="instructions">Instructions go here.</div>
<div class="container">
  <div class="button">This is Button 1</div>
  <div class="button">Thar be Button 2</div>
  <div class="button">Yarr, button 3</div>
  <div class="button">Hey that is a nice looking button.</div>
  <div class="button">Click Me!</div>
</div>
Run Code Online (Sandbox Code Playgroud)

或者,你可以限制的高度body100vh,使之display: flex; flex-direction: column与集flex-grow: 1.container,因此会占用的可用空间。

* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
}

body {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.instructions {
  height: 100px;
  background: linear-gradient(to bottom, #ffffff 0%, #999 100%);
}

.container {
  display: flex;
  flex-direction: column;
  flex-wrap: wrap;
  flex-grow: 1;
}

.button {
  width: 200px;
  height: 50px;
  margin: 10px;
  background: linear-gradient(to bottom, #ffffff 0%, #BBB 100%);
  border: 1px solid #CCC;
  text-align: center;
}
Run Code Online (Sandbox Code Playgroud)
<div class="instructions">Instructions go here.</div>
<div class="container">
  <div class="button">This is Button 1</div>
  <div class="button">Thar be Button 2</div>
  <div class="button">Yarr, button 3</div>
  <div class="button">Hey that is a nice looking button.</div>
  <div class="button">Click Me!</div>
</div>
Run Code Online (Sandbox Code Playgroud)