Flex-box: Align last row to grid

后端 未结 29 2534
不知归路
不知归路 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:21

    One technique would be inserting a number of extra elements (as many as the max number of elements you ever expect to have in a row) that are given zero height. Space is still divided, but superfluous rows collapse to nothing:

    http://codepen.io/dalgard/pen/Dbnus

    body {
      padding: 5%;
    }
    
    div {
      overflow: hidden;
      background-color: yellow;
    }
    
    ul {
      display: flex;
      flex-wrap: wrap;
      margin: 0 -4px -4px 0;
      list-style: none;
      padding: 0;
    }
    
    li {
      flex: 1 0 200px;
      height: 200px;
      border-right: 4px solid black;
      border-bottom: 4px solid black;
      background-color: deeppink;
    }
    li:empty {
      height: 0;
      border: none;
    }
    
    *,
    :before,
    :after {
      box-sizing: border-box;
    }
    • a
    • b
    • c
    • d
    • e
    • f
    • g
    • h
    • i
    • j
    • k

    In the future, this may become achievable through using multiple ::after(n).

提交回复
热议问题