【问题标题】:How to make all children in a flex-container to have the same height如何使弹性容器中的所有孩子都具有相同的高度
【发布时间】:2017-03-22 14:09:34
【问题描述】:

我被这个问题困住了。

我正在尝试使 flex 容器中的所有子 div 具有与最高子 div 相同的高度,这里是 100px,我希望它们居中对齐

不知道我该怎么做。我试过 flex:1 不起作用,我也试过做 align:stretch 但似乎不起作用。

有人可以帮忙吗?

.container {
  display: flex;
  flex-direction: row;
  border: 1px solid black;
  width: 500px;
  align-items: center;
  height:160px;
}
.container div:not(:first-child) {
  background: green;
  margin-top: 20px;
  margin-left: 20px;

}
.container div:first-child {
  height: 100px;
  background: red;
  margin-top: 20px;
  margin-left: 20px;
  text-align: center;
}
<div class="container">
  <div>
    item-1
  </div>
  <div>
    item-2
  </div>
  <div>
    item-3
  </div>
</div>

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    为此,您只需将内部 div 包裹在另一个 div 中并在 container 元素上设置 align-items: center

    .container {
      display: flex;
      border: 1px solid black;
      align-items: center;
      width: 500px;
      height: 150px;
    }
    div > div {
      display: flex;
    }
    .container div div:not(:first-child) {
      background: green;
      margin-left: 20px;
    }
    .container div div:first-child {
      height: 100px;
      background: red;
      margin-left: 20px;
      text-align: center;
    }
    <div class="container">
      <div>
        <div>
          item-1
        </div>
        <div>
          item-2
        </div>
        <div>
          item-3
        </div>
      </div>
    </div>

    【讨论】:

    • justify-content:flex-end 它在主轴上定义..我正在寻找交叉轴 flex-end 的东西
    • 取决于弹性方向。
    • 是的...这里是 flex-direction:row,我正在寻找 align-items:flex-end 但所有项目都应该采用最高项目的高度
    • 您没有在容器上设置高度,因此高度取决于最高项目,因此所有项目默认对齐到容器底部。
    • 哦,是的..我明白了..我已经修改了我的问题,实际上当它必须是 align-items:center 并且所有项目都必须具有相同的高度时,我们该怎么做
    猜你喜欢
    • 2020-12-15
    • 2019-10-02
    • 2015-05-08
    • 1970-01-01
    • 2021-11-27
    • 2018-04-15
    • 2018-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多