css grid problem with background and wrapping

前端 未结 1 668
醉梦人生
醉梦人生 2020-12-21 14:58

I have a grid where I want it to work somewhat like it is except when there are only 1 or 2 cells I want it to auto size and not be a set of 3 with the last one holding its

相关标签:
1条回答
  • 2020-12-21 15:09

    So you want to make the grid-container auto-sized less that 3 items and wrap after that - here's a solution that uses implicit grids.

    • use a single column inline grid container:

      display: inline-grid;
      grid-template-columns: 100px;
      grid-template-rows: 100px;
      
    • size your implicit grids as 100px x 100px using:

      grid-auto-columns: 100px;
      grid-auto-rows: 100px;
      
    • place your second item to the second row using grid-column: 2; and your third item to the third row using grid-column: 3

    Now you will have a 3 x 3 grid - see demo below:

    * {
      box-sizing: border-box;
    }
    
    .parent {
      background: #f4f4f5;
      font-size: 2em;
      border: 1px solid black;
      padding: 20px 0;
      text-align: center;
      width: 100%;
    }
    
    .parent .wrapper {
      display: inline-grid;
      grid-template-columns: 100px; /* one column layout */
      grid-template-rows: 100px; /* sizes the rows */
      grid-auto-columns: 100px; /* sizing columns of implicit grid */
      grid-auto-rows: 100px; /* sizing rows of implicit grid */
      box-shadow: 1px 0px 5px 0px rgba(0, 0, 0, 0.75);
      grid-auto-flow: row;
      background-color: white;
    }
    
    .cell:nth-child(2) {
      grid-column: 2; /* places it in the second column */
    }
    
    .cell:nth-child(3) { 
      grid-column: 3; /* places it in the third column */
    }
    
    .cell {
      border: 1px solid gray;
      background-color: white;
    }
    
    .cell .inner {
      display: flex;
      justify-content: center;
      align-items: center;
      /* width: 100px; */
      /* height: 100px; */
      height: 100%; /* added */
      text-align: center;
    }
    <div class="parent">
      <div class="wrapper">
        <div class="cell">
          <div class="inner">Cell 1</div>
        </div>
      </div>
    </div>
    
    <div class="parent">
      <div class="wrapper">
        <div class="cell">
          <div class="inner">Cell 1</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 2</div>
        </div>
      </div>
    </div>
    
    <div class="parent">
      <div class="wrapper">
        <div class="cell">
          <div class="inner">Cell 1</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 2</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 3</div>
        </div>
      </div>
    </div>
    
    <div class="parent">
      <div class="wrapper">
        <div class="cell">
          <div class="inner">Cell 1</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 2</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 3</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 4</div>
        </div>
      </div>
    </div>
    
    <div class="parent">
      <div class="wrapper">
        <div class="cell">
          <div class="inner">Cell 1</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 2</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 3</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 4</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 5</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 6</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 7</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 8</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 9</div>
        </div>
        <div class="cell">
          <div class="inner">Cell 10</div>
        </div>
      </div>
    </div>

    0 讨论(0)
提交回复
热议问题