Equal height rows in a flex container

前端 未结 9 624
梦如初夏
梦如初夏 2020-11-22 09:28

As you can see, the list-items in the first row have same height. But items in the second row have different height

相关标签:
9条回答
  • 2020-11-22 09:50

    The answer is NO.

    The reason is provided in the flexbox specification:

    6. Flex Lines

    In a multi-line flex container, the cross size of each line is the minimum size necessary to contain the flex items on the line.

    In other words, when there are multiple lines in a row-based flex container, the height of each line (the "cross size") is the minimum height necessary to contain the flex items on the line.

    Equal height rows, however, are possible in CSS Grid Layout:

    • Equal height rows in CSS Grid Layout

    Otherwise, consider a JavaScript alternative.

    0 讨论(0)
  • 2020-11-22 09:51

    This seems to be working in cases with fixed parent height (tested in Chrome and Firefox):

    .child {
            height    : 100%;
            overflow  : hidden;
    }
    
    .parent {
            display: flex;
            flex-direction: column;
            height: 70vh; // ! won't work unless parent container height is set
            position: relative;
    }
    

    If it's not possible to use grids for some reason, maybe it's the solution.

    0 讨论(0)
  • 2020-11-22 09:52

    you can do it by fixing the height of "P" tag or fixing the height of "list-item".

    I have done by fixing the height of "P"

    and overflow should be hidden.

    .list{
      display: flex;
      flex-wrap: wrap;
      max-width: 500px;
    }
    
    .list-item{
      background-color: #ccc;
      display: flex;
      padding: 0.5em;
      width: 25%;
      margin-right: 1%;
      margin-bottom: 20px;
    }
    .list-content{
      width: 100%;
    }
    p{height:100px;overflow:hidden;}
    <ul class="list">
      <li class="list-item">
        <div class="list-content">
        <h2>box 1</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
        </div>
      </li>
      <li class="list-item">
        <div class="list-content">
        <h3>box 2</h3>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
        </div>
      </li>
      
        <li class="list-item">
        <div class="list-content">
        <h3>box 2</h3>
        <p>Lorem ipsum dolor</p>
        </div>
      </li>
      
        <li class="list-item">
        <div class="list-content">
        <h3>box 2</h3>
        <p>Lorem ipsum dolor</p>
        </div>
      </li>
      <li class="list-item">
        <div class="list-content">
          <h1>h1</h1>
        </div>
      </li>
    </ul>

    0 讨论(0)
  • 2020-11-22 09:54

    In your case height will be calculated automatically, so you have to provide the height
    use this

    .list-content{
      width: 100%;
      height:150px;
    }
    
    0 讨论(0)
  • 2020-11-22 09:56

    for same height you should chage your css "display" Properties. For more detail see given example.

    .list{
      display: table;
      flex-wrap: wrap;
      max-width: 500px;
    }
    
    .list-item{
      background-color: #ccc;
      display: table-cell;
      padding: 0.5em;
      width: 25%;
      margin-right: 1%;
      margin-bottom: 20px;
    }
    .list-content{
      width: 100%;
    }
    <ul class="list">
      <li class="list-item">
        <div class="list-content">
        <h2>box 1</h2>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit. </p>
        </div>
      </li>
      <li class="list-item">
        <div class="list-content">
        <h3>box 2</h3>
        <p>Lorem ipsum dolor sit amet, consectetur adipisicing elit.</p>
        </div>
      </li>
      
        <li class="list-item">
        <div class="list-content">
        <h3>box 2</h3>
        <p>Lorem ipsum dolor</p>
        </div>
      </li>
      
        <li class="list-item">
        <div class="list-content">
        <h3>box 2</h3>
        <p>Lorem ipsum dolor</p>
        </div>
      </li>
      <li class="list-item">
        <div class="list-content">
          <h1>h1</h1>
        </div>
      </li>

    0 讨论(0)
  • 2020-11-22 10:06

    If you know the items you are mapping through, you can accomplish this by doing one row at a time. I know it's a workaround, but it works.

    For me I had 4 items per row, so I broke it up into two rows of 4 with each row height: 50%, get rid of flex-grow, have <RowOne /> and <RowTwo /> in a <div> with flex-column. This will do the trick

    <div class='flexbox flex-column height-100-percent'>
       <RowOne class='flex height-50-percent' />
       <RowTwo class='flex height-50-percent' />
    </div>
    
    0 讨论(0)
提交回复
热议问题