css - 如何设置 flexbox 容器的高度以便它使用 "remaining space"

标签 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>

最佳答案

你必须给这个部分一个高度来限制它使按钮环绕,否则 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;
}
<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 的高度至 100vh , 让它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;
}
<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>

关于css - 如何设置 flexbox 容器的高度以便它使用 "remaining space",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42868808/

相关文章:

jquery - CSS 淡入淡出 onclick

c++ - Qt样式表的任意检查

javascript - 手动将 dirty 设置为字段不会修改 css 类 angularJS?

html - Bootstrap 3.3.7 "row"导致水平滚动条

css - Flexbox:Autoprefixer 添加了一个破坏 Safari 的 css 属性

html - 如何使 flex 响应

html - 空白 :nowrap expands div?

jquery - 从单击的 div 中心缩放弹出窗口到中心屏幕

android - 将 ionic v3 按钮高度设置为连续 100% 高度

html - 为什么这个 flex table 适用于 chrome,但不适用于 IE 11?