Flex-box: Align last row to grid

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

    There is a way without flexbox, although you'd need to meet the following conditions. 1) The container has padding. 2) Items are the same size and you know exactly how many you want per line.

    ul {
      padding: 0 3% 0 5%;
    }
    li {
      display: inline-block; 
      padding: 0 2% 2% 0;
      width: 28.66%;
    }
    

    The smaller padding on the right side of the container allows for the extra padding to the right of each list item. Assuming other items in the same parent as the list object are padded with 0 5%, it will be flush with them. You can also adjust the percentages to however much margin you'd like or use calculate px values.

    Of course, you can do the same without the padding on the container by using nth-child (IE 9+) to remove margin on every third box.

提交回复
热议问题