使flexbox儿童具有相同的高度

时间:2018-04-10 04:41:55

标签: css flexbox

我有一个flexbox父级设置了flex-direction:row。 在这个家长里面,我有两个孩子。我希望他们有相同的高度!

在这些孩子中,我有动态内容(可变高度)。

我正在做的方式,如果我在正确的孩子上添加文字,左边的一个会增长。 但如果左孩子长大,那么右孩子就会变小。

他们不应该以同样的方式行事吗?

这是一个小提琴:https://jsfiddle.net/4g6uevok/8/

HTML:

<div id="main">
  <div class="left">
    <div class="title">MY TITLE:</div>
    <div class="left-area">

      <div class="left-area-row">
        <div class="left-area-row-titulo">#1</div>
        <div class="left-area-row-info">A</div>
      </div>
      <div class="left-area-row">
        <div class="left-area-row-titulo">#2</div>
        <div class="left-area-row-info">B</div>
      </div>
      <div class="left-area-row">
        <div class="left-area-row-titulo">#3</div>
        <div class="left-area-row-info">AC</div>
      </div>

    </div>
  </div>

  <div class="right">
    <div class="title">SECOND TITLE:</div>
  </div>
</div>

CSS:

#main {
    width: 100%;
    height:auto;
    margin-top: 30px;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
    align-items: stretch;
    background-color: red;
}

.left{
    width: 400px;
    display: flex;
    flex-direction: column;     
    background:lime;
    align-items: stretch;
}

.title {
    width: 100%;
    font-size: 1.5em;
    color:#525252;
    display: flex;
    justify-content: center;
    margin-bottom: 20px;
    font-weight: bold;
    font-family: "emir-bold";
}

.left-area {
    width: 100%;
    display: flex;
    flex-direction: column;
}
.left-area-row {
    width: 100%;
    display: flex;
    flex-direction: row;
    justify-content: space-between;
}
.left-area-row-titulo {
    width: 49.5%;
    display: flex;
    align-items: center;
    justify-content: flex-start;
    background-color: #819196;
    color: white;
    padding: 6px;
    margin:0 2px 4px 0;
}
.left-area-row-info {
    width: 49.5%;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #CCCCCC;
    padding: 6px;
    margin:0 0 4px 2px;
}

.right {
    width: calc(100% - 430px);
    height: 100%;
    display: flex;
    flex-direction: column;
    background:orange;
    align-items: stretch;
}

1 个答案:

答案 0 :(得分:0)

默认情况下,Flex项目与strech对齐。 height:100%课程中的.right值会阻止其占据整个高度,因此请尝试将height:100%移至.right元素

#main {
  width: 100%;
  height: auto;
  margin-top: 30px;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
  align-items: stretch;
  background-color: red;
}

.left {
  width: 400px;
  display: flex;
  flex-direction: column;
  background: lime;
  align-items: stretch;
}

.title {
  width: 100%;
  font-size: 1.5em;
  color: #525252;
  display: flex;
  justify-content: center;
  margin-bottom: 20px;
  font-weight: bold;
  font-family: "emir-bold";
}

.left-area {
  width: 100%;
  display: flex;
  flex-direction: column;
}

.left-area-row {
  width: 100%;
  display: flex;
  flex-direction: row;
  justify-content: space-between;
}

.left-area-row-titulo {
  width: 49.5%;
  display: flex;
  align-items: center;
  justify-content: flex-start;
  background-color: #819196;
  color: white;
  padding: 6px;
  margin: 0 2px 4px 0;
}

.left-area-row-info {
  width: 49.5%;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #CCCCCC;
  padding: 6px;
  margin: 0 0 4px 2px;
}

.right {
  width: calc(100% - 430px);
  display: flex;
  flex-direction: column;
  background: orange;
  align-items: stretch;
}
<div id="main">
  <div class="left">
    <div class="title">MY TITLE:</div>
    <div class="left-area">
      <div class="left-area-row">
        <div class="left-area-row-titulo">
          #1
        </div>
        <div class="left-area-row-info">A</div>
      </div>
      <div class="left-area-row">
        <div class="left-area-row-titulo">
          #2
        </div>
        <div class="left-area-row-info">BA</div>
      </div>
      <div class="left-area-row">
        <div class="left-area-row-titulo">
          #3
        </div>
        <div class="left-area-row-info">C</div>
      </div>
      <div class="left-area-row">
        <div class="left-area-row-titulo">
          #4
        </div>
        <div class="left-area-row-info">D</div>
      </div>
    </div>
  </div>
  <div class="right">
    <div class="title">SECOND TITLE:</div>
  </div>
</div>