Flex-grow不适用于列方向

时间:2018-09-07 14:08:09

标签: html css css3 flexbox

我正在尝试学习flexbox的布局。我看到一个教程谈到了flex-grow和flex-shrink类,本课程中提供的所有示例都可以在flex-direction:row上很好地工作。

我尝试将它们与flex-direction: column一起应用,但是它并没有按照我的要求工作。我用谷歌搜索,但是当方向设置为列时,找不到合适的答案来增加弹性项目。

我的代码如下。谁能修复我的CSS,以便在列方向上进行flex-grow / shrink工作?

html,
body {
    padding: 0;
    margin: 0;
    font-family: arial, sans-serif;
}

.flex-container {
    display: flex;
    border:solid 4px #000;
    flex-direction: column;
    height: 100%;

}

.flex-item {
    color: #eee;
    font-size: 1.2em;
    padding: 1em;
    text-align: center;
    justify-content: center;
    flex-grow: 1;
}

.flex-item:nth-child(1) {
    background-color: #A62E5C;
    flex-grow: 3;
}

.flex-item:nth-child(2) {
    background-color: #9BC850;
}

.flex-item:nth-child(3) {
    background-color: #675BA7;
}
.flex-item:nth-child(4) {
    background-color: #620542;
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
          integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
    <link rel="stylesheet" type="text/css" href="style.css">
</head>
<body>
    <div class="container-fluid">
        <div class="flex-container">
            <div class="flex-item">Flex 1</div>
            <div class="flex-item">Flex 2</div>
            <div class="flex-item">Flex 3</div>
            <div class="flex-item">Flex 4</div>
        </div>
    </div>
</body>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>
</html>

1 个答案:

答案 0 :(得分:2)

它可以工作,但是您不会看到任何不同,因为flex-grow会考虑可用空间来使元素增长,并且默认情况下,列方向上没有可用空间,因为高度由内容定义,与使用行方向时不同,在行方向上宽度通常为100%并且我们有可用空间。

另外,在您的情况下,您使用了height:100%,并且由于在父元素上未定义高度,它将回退到auto,因此它将是内容的高度,并且上面的逻辑将适用

使用固定的高度,您将看到效果:

html,
body {
  padding: 0;
  margin: 0;
  font-family: arial, sans-serif;
}

.flex-container {
  display: flex;
  border: solid 4px #000;
  flex-direction: column;
  height: 600px;
}

.flex-item {
  color: #eee;
  font-size: 1.2em;
  padding: 1em;
  text-align: center;
  justify-content: center;
  flex-grow: 1;
}

.flex-item:nth-child(1) {
  background-color: #A62E5C;
  flex-grow: 3;
}

.flex-item:nth-child(2) {
  background-color: #9BC850;
}

.flex-item:nth-child(3) {
  background-color: #675BA7;
}

.flex-item:nth-child(4) {
  background-color: #620542;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
  <div class="container-fluid">
    <div class="flex-container">
      <div class="flex-item">Flex 1</div>
      <div class="flex-item">Flex 2</div>
      <div class="flex-item">Flex 3</div>
      <div class="flex-item">Flex 4</div>
    </div>
  </div>
</body>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</html>

并且为了修复height:100%,您需要将其添加到父元素:

html,
body {
  padding: 0;
  margin: 0;
  font-family: arial, sans-serif;
  height: 100%;
}
.container-fluid {
  height: 100%;
}
.flex-container {
  display: flex;
  border: solid 4px #000;
  flex-direction: column;
  height: 100%;
}

.flex-item {
  color: #eee;
  font-size: 1.2em;
  padding: 1em;
  text-align: center;
  justify-content: center;
  flex-grow: 1;
}

.flex-item:nth-child(1) {
  background-color: #A62E5C;
  flex-grow: 3;
}

.flex-item:nth-child(2) {
  background-color: #9BC850;
}

.flex-item:nth-child(3) {
  background-color: #675BA7;
}

.flex-item:nth-child(4) {
  background-color: #620542;
}
<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8">
  <title>Title</title>
  <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
  <link rel="stylesheet" type="text/css" href="style.css">
</head>

<body>
  <div class="container-fluid">
    <div class="flex-container">
      <div class="flex-item">Flex 1</div>
      <div class="flex-item">Flex 2</div>
      <div class="flex-item">Flex 3</div>
      <div class="flex-item">Flex 4</div>
    </div>
  </div>
</body>
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js" integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js" integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q" crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl" crossorigin="anonymous"></script>

</html>


  

flex-grow CSS属性指定flex的flex增长因子   项目。它指定flex中的多少可用空间   容器应该分配给该项目 ref