CSS only solution to set “same height” row sections on a responsive grid

前端 未结 1 1303
离开以前
离开以前 2021-01-14 01:50

Wanted: a CSS only solution to enable equal height grid \"sections\" on a per row basis, that is also responsive.

This diagram hopefully explains th

相关标签:
1条回答
  • 2021-01-14 02:24

    By giving the .items display: flex; flex-wrap: wrap; your item will become flex items and flow from left to right and wrap when there is no more space.

    Then you give the .item display: flex; flex-direction: column;, which will make them a flex container as well, and by using column direction, its children will flow vertically, like block elements.

    Finally you give the .item__content flex: 1;, which will make it take any remaining space, vertically, hence every row's item will have equal height.

    Updated codepen

    Stack snippet

    .items {
      display: flex;
      flex-wrap: wrap;
      max-width: 1200px;
    }
    
    .item {
      display: flex;
      flex-direction: column;
      width: 25%;
      box-sizing: border-box;
      vertical-align: top;
      padding: 0 12px;
      margin: 24px -4px 24px 0;
    }
    
    @media (max-width: 600px) {
      .item {
        width: 50%;
      }
    }
    
    .item__heading {
      background-color: #d4d0f5;
      padding: 10px;
      text-align: center;
      border: 1px solid #bbbbbb;
    }
    
    .item__content {
      flex: 1 1 auto;                   /* IE need 1 1 auto */
      padding: 10px;
      border-left: 1px solid #bbbbbb;
      border-right: 1px solid #bbbbbb;
    }
    
    .item__price {
      background-color: #e0f6d9;
      padding: 10px;
      text-align: center;
      border: 1px solid #bbbbbb;
    }
    <div class="items">
    
      <div class="item">
        <div class="item__heading">
          Item 1
        </div>
        <div class="item__content">
          Some content that is not that long
        </div>
        <div class="item__price">
          £99.99
        </div>
      </div>
    
      <div class="item">
        <div class="item__heading">
          Item 2
        </div>
        <div class="item__content">
          Some content that is longer than other items on the same row and sets the height of this section
        </div>
        <div class="item__price">
          £69.99
        </div>
      </div>
    
      <div class="item">
        <div class="item__heading">
          Item 3
        </div>
        <div class="item__content">
          Some content that is not that long
        </div>
        <div class="item__price">
          £69.99
        </div>
      </div>
    
      <div class="item">
        <div class="item__heading">
          Item 4
        </div>
        <div class="item__content">
          Some content that is not that long
        </div>
        <div class="item__price">
          £109.99
        </div>
      </div>
    
      <div class="item">
        <div class="item__heading">
          Item 5
        </div>
        <div class="item__content">
          Some content that is a medium kind of length blah blah
        </div>
        <div class="item__price">
          £29.99
        </div>
      </div>
    
      <div class="item">
        <div class="item__heading">
          Item 6
        </div>
        <div class="item__content">
          Some content that is not that long
        </div>
        <div class="item__price">
          £99.99
        </div>
      </div>
    
    </div>

    0 讨论(0)
提交回复
热议问题