Flex-box: Align last row to grid

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

    I modified the example presented by Dan Andreasson by using a right border on elements to create a faux gutter. You can then use nth-child to remove the border on the last-child of the column grid count you need. here is a demo https://codepen.io/JacobLett/pen/mdVoroM

    /* demo only */
    body {
    margin:0;
    padding:0;
    max-width:1024px;
    margin:0 auto;
    }
    .block-list {
    background: #ccc;
      border:1px solid #ccc;
    }
    
    .block-list .block-list__item {
    background: #eee;
    }
    /* demo only */
    
    
    
    
    .block-list .block-list__item {
       min-height: 100px;
       margin-bottom: 1rem;
    }
    
    @media only screen and (min-width: 900px) {
       .block-list {
          display: -webkit-box;
          display: flex;
          flex-wrap: wrap;
          -webkit-box-pack: justify;
          justify-content: space-between;
          background-color: #ffffff;
          margin: 1em auto;
       }
    
       .block-list:after {
          content: "";
          -webkit-box-flex: 1;
          flex: auto;
       }
    
       .block-list__item {
          height: 10em;
          width: 25%;
          box-sizing: border-box;
          border-right: 10px solid white;
       }
    
       .block-list-2 .block-list__item {
          width: 50%;
       }
    
       .block-list-2 .block-list__item:nth-child(2n) {
          border: none;
       }
    
       .block-list-3 .block-list__item {
          width: 33.3%;
       }
    
       .block-list-3 .block-list__item:nth-child(3n) {
          border: none;
       }
    
       .block-list-4 .block-list__item {
          width: 25%;
       }
    
       .block-list-4 .block-list__item:nth-child(4n) {
          border: none;
       }
       
       .block-list-5 .block-list__item {
          width: 20%;
       }
    
       .block-list-5 .block-list__item:nth-child(5n) {
          border: none;
       }
       
       .block-list-6 .block-list__item {
          width: 16.66%;
       }
    
       .block-list-6 .block-list__item:nth-child(6n) {
          border: none;
       }
    }

    2 column

    1
    2
    3
    4
    5
    6

    3 column

    1
    2
    3
    4
    5
    6

    4 column

    1
    2
    3
    4
    5
    6

    5 column

    1
    2
    3
    4
    5
    6

    6 column

    1
    2
    3
    4
    5
    6

提交回复
热议问题