Flex-box: Align last row to grid

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

    I made a SCSS mixin for it.

    @mixin last-row-flexbox($num-columns, $width-items){
    
      $filled-space: $width-items * $num-columns;
      $margin: calc((100% - #{$filled-space}) / (#{$num-columns} - 1));
    
      $num-cols-1 : $num-columns - 1;
    
      &:nth-child(#{$num-columns}n+1):nth-last-child(-n+#{$num-cols-1}) ~ & {
        margin-left: $margin;
      }
      @for $i from 1 through $num-columns - 2 { 
        $index: $num-columns - $i;
        &:nth-child(#{$num-columns}n+#{$index}):last-child{
          margin-right: auto;
        }
      }
    }
    

    This is the codepen link: http://codepen.io/diana_aceves/pen/KVGNZg

    You just have to set the items width in percentage and number of columns.

    I hope this can help you.

提交回复
热议问题