Flex-box: Align last row to grid

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

    A possible solution is to use justify-content: flex-start; on the .grid container, size restrictions on its children, and margins on the appropriate child elements -- depending on the desired number of columns.

    For a 3-column grid, the basic CSS would look like this:

    .grid {
        display: flex;
        flex-flow: row wrap;
        justify-content: flex-start;
    }
    
    .grid > * {
        flex: 0 0 32%;
        margin: 1% 0;
    }
    
    .grid > :nth-child(3n-1) {
        margin-left: 2%;
        margin-right: 2%;
    }
    

    It's another imperfect solution, but it works.

    http://codepen.io/tuxsudo/pen/VYERQJ

提交回复
热议问题