Flex-box: Align last row to grid

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

    If you want to align the last item to the grid use the following code:

    Grid container

    .card-grid {
      box-sizing: border-box;
      max-height: 100%;
      display: flex;
      flex-direction: row;
      -webkit-box-orient: horizontal;
      -webkit-box-direction: normal;
      justify-content: space-between;
      align-items: stretch;
      align-content: stretch;
      -webkit-box-align: stretch;
      -webkit-box-orient: horizontal;
      -webkit-box-direction: normal;
      -ms-flex-flow: row wrap;
      flex-flow: row wrap;
    }
    
    .card-grid:after {
      content: "";
      flex: 1 1 100%;
      max-width: 32%;
    }
    

    Item in the grid

    .card {
      flex: 1 1 100%;
      box-sizing: border-box;
      -webkit-box-flex: 1;
      max-width: 32%;
      display: block;
      position: relative;
    
    }
    

    The trick is to set the max-width of the item equal to the max-width of the .card-grid:after.

    Live demo on Codepen

提交回复
热议问题