Using CSS, how to create a two-column grid with equal-width columns that are “only as narrow as required”?

前端 未结 2 1307
后悔当初
后悔当初 2021-01-20 03:18

Assuming I have a piece of HTML that is structured like this:



2条回答
  •  半阙折子戏
    2021-01-20 03:51

    Just revisited this now since CSS Grid has become much more supported. One solution for CSS Grid would be this:

    .linkgrid {
      display: inline-grid;
      grid-template-columns: repeat(2, 1fr);
      column-gap: 10px;
      row-gap: 10px;
    }
    
    .gridentry > * {
      display: block;
    
      padding: 10px;
    
      text-align: center;
      
      background-color: red;
      color: white;
    
      /* This is helpful if the texts get too long to break them "naturally": */
      /* word-break: break-all; */
    }

提交回复
热议问题