Take 2 columns in 2-columns layout but not when 1-column layout in CSS grid without @media

前端 未结 3 1661
面向向阳花
面向向阳花 2021-01-25 09:09

The desired layout for wide screens:

The desired layout for narrow screens:

Initial CSS:

.La         


        
3条回答
  •  無奈伤痛
    2021-01-25 09:36

    Use grid-column: 1/-1;

    .box {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
      grid-gap: 24px 40px;
    }
    
    span {
      height: 50px;
      background: blue;
    }
    
    .more {
      grid-column: 1/-1;
      background: red;
    }

    That you can edit like below if you want to always have a maximum of 2 columns:

    .box {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(max(240px,40vw), 1fr));
      grid-gap: 24px 40px;
    }
    
    span {
      height: 50px;
      background: blue;
    }
    
    .more {
      grid-column: 1/-1;
      background: red;
    }

提交回复
热议问题