Flex-box: Align last row to grid

后端 未结 29 2476
不知归路
不知归路 2020-11-22 02:54

I have a simple flex-box layout with a container like:

.grid {
  display: flex;
  flex-flow: row wrap;
  justify-content: space-between;
}

相关标签:
29条回答
  • 2020-11-22 03:16

    Oh boy, I think I found a good solution with minimal CSS and no JS. Check it out:

    img {width:100%;}
    li {
      display: inline-block;
      width:8em;
      list-style:none;
    }
    ul {text-align: justify;}
    <ul>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li>
        <img src="http://www.planwallpaper.com/static/images/kitty-cat.jpg" />
      </li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
      <li></li>
    </ul>

    The key here is to remember that what we are trying to achieve is exactly what text-align: justify does!

    The empty elements in the HTML are there to make the last row display perfectly without changing the appearance, but might not be needed given what you are trying to achieve. For perfect balance in every situation, you need at least x-4 empty elements, x being the number of elements to display, or n-2, n being the number of column you want to display.

    0 讨论(0)
  • 2020-11-22 03:16

    Assuming:

    • You want 4 column grid layout with wrapping
    • The number of items is not necessarily a multiple of 4

    Set a left margin on every item except 1st, 5th and 9th item and so on. If the left margin is 10px then each row will have 30px margin distributed among 4 items. The percentage width for item is calculated as follows:

    100% / 4 - horizontal-border - horizontal-padding - left-margin * (4 - 1) / 4
    

    This is a decent workaround for issues involving last row of flexbox.

    .flex {
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      margin: 1em 0 3em;
      background-color: peachpuff;
    }
    
    .item {
      margin-left: 10px;
      border: 1px solid;
      padding: 10px;
      width: calc(100% / 4 - 2px - 20px - 10px * (4 - 1) / 4);
      background-color: papayawhip;
    }
    
    .item:nth-child(4n + 1) {
      margin-left: 0;
    }
    
    .item:nth-child(n + 5) {
      margin-top: 10px;
    }
    <div class="flex">
      <div class="item">1</div>
      <div class="item">2</div>
      <div class="item">3</div>
      <div class="item">4</div>
    </div>
    <div class="flex">
      <div class="item">1</div>
      <div class="item">2</div>
      <div class="item">3</div>
      <div class="item">4</div>
      <div class="item">5</div>
      <div class="item">6</div>
    </div>
    <div class="flex">
      <div class="item">1</div>
      <div class="item">2</div>
      <div class="item">3</div>
      <div class="item">4</div>
      <div class="item">5</div>
      <div class="item">6</div>
      <div class="item">7</div>
      <div class="item">8</div>
      <div class="item">9</div>
    </div>

    0 讨论(0)
  • 2020-11-22 03:17

    This problem was solved for me using CSS grid,

    This solution is applicable only if you're having fix number of columns i.e. no. of elements to display in a single row

    -> using grid but not specifying number of rows, as number of elements increase it wraps into columns and add rows dynamically, I have specified three columns in this example

    -> you don't have to give any position to your child/cells, as it will make it fix, which we don't want.

    .grid-class{
      display: grid;
      grid-template-columns: repeat(3, 1fr);
      column-gap: 80px;
    }

    0 讨论(0)
  • 2020-11-22 03:18

    Add a ::after which autofills the space. No need to pollute your HTML. Here is a codepen showing it: http://codepen.io/DanAndreasson/pen/ZQXLXj

    .grid {
      display: flex;
      flex-flow: row wrap;
      justify-content: space-between;
    }
    
    .grid::after {
      content: "";
      flex: auto;
    }
    
    0 讨论(0)
  • 2020-11-22 03:18

    You can't. Flexbox is not a grid system. It does not have the language constructs to do what you're asking for, at least not if you're using justify-content: space-between. The closest you can get with Flexbox is to use the column orientation, which requires setting an explicit height:

    http://cssdeck.com/labs/pvsn6t4z (note: prefixes not included)

    ul {
      display: flex;
      flex-flow: column wrap;
      align-content: space-between;
      height: 4em;
    }
    

    However, it would be simpler to just use columns, which has better support and doesn't require setting a specific height:

    http://cssdeck.com/labs/dwq3x6vr (note: prefixes not included)

    ul {
      columns: 15em;
    }
    
    0 讨论(0)
  • 2020-11-22 03:18

    Just add few fake items with same properties except for height set to 0px to the end.

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