Flex-box: Align last row to grid

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

    It is possible to use "flex-start" and to add the margins manually. It requires some math-hacking but is definitely easy to do and make it easy to use with a CSS preprocessor like LESS.

    See for example this LESS mixin:

    .flexboxGridMixin(@columnNumber,@spacingPercent) {
      @contentPercent: 100% - @spacingPercent;
      @sideMargin: @spacingPercent/(@columnNumber*2);
      display: flex;
      flex-direction: row;
      flex-wrap: wrap;
      justify-content: flex-start;
      > * {
        box-sizing: border-box;
        width: @contentPercent/@columnNumber;
        margin-left: @sideMargin;
        margin-right: @sideMargin;
      }
    }
    

    And then it can easily be used to display a responsive grid layout:

    ul {
      list-style: none;
      padding: 0;
      @spacing: 10%;
      @media only screen and (max-width: 499px) { .flexboxGridMixin(1,@spacing); }
      @media only screen and (min-width: 500px) { .flexboxGridMixin(2,@spacing); }
      @media only screen and (min-width: 700px) { .flexboxGridMixin(3,@spacing); }
      @media only screen and (min-width: 900px) { .flexboxGridMixin(4,@spacing); }
      @media only screen and (min-width: 1100px) { .flexboxGridMixin(5,@spacing); }
    }
    
    li {
      background: pink;
      height: 100px;
      margin-top: 20px;
    }
    

    Here is an example of

    http://codepen.io/anon/pen/YyLqVB?editors=110

提交回复
热议问题