flex-direction: row - children of same height

2020-04-21 03:40发布

问题:

How is it possible to get the .childs to all have the same height. height: 100% is not working an neither is flex-grow: 1. What is the recommended approach for this using flexbox?

.parent {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
    width: 800px;
}

.child {
  width: 150px;
  background-color: green;
  border: solid 1px black;
}
<div class="parent">
  <div class="child">
    Short Text
  </div>
  <div class="child">
    Long Text: Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla
  </div>
  <div class="child">
    Short Text
  </div>
</div>

回答1:

.parent {
    display: flex;
    flex-direction: row;
    width: 800px;
    justify-content: center;
    align-items: stretch;
}

.child {
  width: 150px;
  background-color: green;
  border: solid 1px black;
}
<div class="parent">
  <div class="child">
    Short Text
  </div>
  <div class="child">
    Long Text: Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla
  </div>
  <div class="child">
    Short Text
  </div>
</div>



回答2:

Remove the align-items:center from the parent and add flex properties to the children for their content alignment.

.parent {
    display: flex;
    flex-direction: row;
    justify-content: center;
    width: 800px;
}

.child {
    display: flex;
    flex-direction: row;
    justify-content: center;
    align-items: center;
  width: 150px;
  background-color: green;
  border: solid 1px black;
}
<div class="parent">
  <div class="child">
    Short Text
  </div>
  <div class="child">
    Long Text: Bla bla bla Bla bla bla Bla bla bla Bla bla bla Bla bla bla
  </div>
  <div class="child">
    Short Text
  </div>
</div>



标签: css flexbox