Flex-box: Align last row to grid

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

    This is pretty hacky, but it works for me. I was trying to achieve consistent spacing/margins.

    .grid {
      width: 1024px;
      display: flex;
      flex-flow: row wrap;
      padding: 32px;
      background-color: #ffffd;  
    
      &:after {
        content: "";
        flex: auto;
        margin-left:-1%;
      }
    
      .item {
        flex: 1 0 24.25%;
        max-width: 24.25%;
        margin-bottom: 10px;
        text-align: center;
        background-color: #bbb;
    
        &:nth-child(4n+2),
        &:nth-child(4n+3),
        &:nth-child(4n+4) {
          margin-left: 1%;
        }
    
        &:nth-child(4n+1):nth-last-child(-n+4),
          &:nth-child(4n+1):nth-last-child(-n+4) ~ .item {
            margin-bottom: 0;
        }    
    
      }
    }
    

    http://codepen.io/rustydev/pen/f7c8920e0beb0ba9a904da7ebd9970ae/

提交回复
热议问题