使用flexbox的并行列高问题

时间:2017-08-17 15:33:50

标签: css flexbox

我有四个div /列,里面有不同数量的文本。使用flexbox我假设我能够将所有列的大小调整为最大的div大小,但是从包含的codepen中你应该看到所有列都以不同的高度显示。

我的最终目标是让它们具有相同的高度,但是当页面调整大小时,它们应该一致地挤压和拉伸,在中间点,列将以2 x 2网格重新排列,并且移动大小全部div被一个堆叠在另一个之上。这部分按预期工作,但4个div仍然有不同的大小。

HTML:

<head>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" integrity="sha384-BVYiiSIFeK1dGmJRAkycuHAHRg32OmUcww7on3RYdg4Va+PmSTsz/K68vbdEjh4u" crossorigin="anonymous">
</head>

<body>
<div class="table-layout">
  <div class="table-cell row">

    <div class="flexboxContainer">

      <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="panel panel-default summary-panel" style="background: green; color: white;">
          <div class="panel-body flexboxContent">

            <div  class="chartContainer">

              <div class="table-layout">
                <div class="table-cell row">
                  <h1>0</h1>
                  <span style="font-size: 14px">blah blah blah blah</span>
                </div>
                <div class="table-cell row">
                  <span style="font-size: 64px; opacity: 0.3"></span>
                </div>
              </div>

            </div>

          </div>
        </div>
      </div>

      <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="panel panel-default summary-panel" style="background: green; color: white;">
          <div class="panel-body flexboxContent">

            <div class="chartContainer">

              <div class="table-layout">
                <div class="table-cell row">
                  <h1>0</h1>
                  <span style="font-size: 14px">blah blah blah blah blah blah blah blah blah blah blah blah</span>
                </div>
                <div class="table-cell row">
                  <span style="font-size: 64px; opacity: 0.3"></span>
                </div>
              </div>

            </div>

          </div>
        </div>
      </div>

      <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="panel panel-default summary-panel" style="background: green; color: white;">
          <div class="panel-body flexboxContent">

            <div  class="chartContainer">

              <div class="table-layout">
                <div class="table-cell row">
                  <h1>0</h1>
                  <span style="font-size: 14px">blah blah blah blah</span>
                </div>
                <div class="table-cell row">
                  <span style="font-size: 64px; opacity: 0.3"></span>
                </div>
              </div>

            </div>

          </div>
        </div>
      </div>

      <div class="col-lg-3 col-md-3 col-sm-6 col-xs-12">
        <div class="panel panel-default summary-panel" style="background: green; color: white;">
          <div class="panel-body flexboxContent">

            <div class="chartContainer">

              <div class="table-layout">
                <div class="table-cell row">
                  <h1>0</h1>
                  <span style="font-size: 14px">blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah blah</span>
                </div>
                <div class="table-cell row">
                  <span style="font-size: 64px; opacity: 0.3"><i class="fa fa-smile-o"></i></span>
                </div>
              </div>

            </div>

          </div>
        </div>
      </div>
    </div>
  </div>
</div>
</body>

CSS:

.flexboxContainer {
  display: flex;
  border: solid red;
  flex-wrap: wrap;
}

.flexboxContent {
  border: solid black;
  flex: 1 1 0;
  align-items: stretch;
}

.table-layout {
  display:table;
  width:100%;
}

.table-layout .table-cell {
  display:table-cell;
}

.chartContainer {
    position: relative;
}

Codepen示例: https://codepen.io/synchrosion/pen/yopxGY

为了清晰起见,我将flexbox容器和flexbox内容用边框突出显示,我试图将所有flexbox内容框放在相同的高度,而不指定固定的最小或最大高度。

非常感谢任何帮助

2 个答案:

答案 0 :(得分:0)

只需添加到您的css:

    .panel-default {
         height: 100%;
    }

答案 1 :(得分:0)

这里是您问题的小提琴,您必须为flexboxContent设置100%的高度

jsfiddle.net/ybtnhzna