Search code examples
htmlcssresponsive-designcss-grid

Is it possible to always have an even number of columns with auto-fill?


Hey there I am new to CSS grid. Have a look at the following example:

.platform {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
  grid-gap: 20px;
}

.item {
  width:100%;
  background-color:#aaa;
  padding:10px;
  box-sizing:border-box;
  text-align:center;
}

.wrapmed {
  max-width:400px;
}

.wrapsmall {
  max-width:300px;
}
<div class="platform">
  <div class="item">a</div>
  <div class="item">b</div>
  <div class="item">c</div>
  <div class="item">d</div>
</div>

<br/><br/>

<div class="wrapmed">
  <div class="platform">
    <div class="item">a</div>
    <div class="item">b</div>
    <div class="item">c</div>
    <div class="item">d</div>
  </div>
</div>

<br/><br/>

<div class="wrapsmall">
  <div class="platform">
    <div class="item">a</div>
    <div class="item">b</div>
    <div class="item">c</div>
    <div class="item">d</div>
  </div>
</div>

Basically the grid works great. However I never want to break the grid such that 3 items are on top and only one item is in the next row.

I want to have either 4 columns, or two colums, or one. (How) can I achieve this behavior?

Edit: The code snippet now shows the three possible cases. Case 2 is unwanted.


Solution

  • You can nest the platform divs to control how the columns will collapse.

    I added a parent platform element that positions two child platform elements either side-by-side or ontop one another.
    And I grouped the items in pairs or two inside of the child platform elements.

    Now when the screen size shrinks, the child platform elements will stack first creating a 2x2 grid.
    If the screen shrinks even more, the items inside the child platforms will stack and create a 1x4 grid.

    enter image description here

    .platform {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(100px, 1fr));
      grid-gap: 20px;
    }
    
    .item {
      width:100%;
      background-color:#aaa;
      padding:10px;
      box-sizing:border-box;
      text-align:center;
    }
    
    .wrapmed {
      max-width:400px;
    }
    
    .wrapsmall {
      max-width:300px;
    }
    <div class="platform">
      <div class="platform">
        <div class="item">a</div>
        <div class="item">b</div>
      </div>
      <div class="platform">
        <div class="item">c</div>
        <div class="item">d</div>
      </div>
    </div>
    
    <br/><br/>
    
    <div class="wrapmed">
      <div class="platform">
        <div class="platform">
          <div class="item">a</div>
          <div class="item">b</div>
        </div>
        <div class="platform">
          <div class="item">c</div>
          <div class="item">d</div>
        </div>
      </div>
    </div>
    
    <br/><br/>
    
    <div class="wrapsmall">
      <div class="platform">
        <div class="platform">
          <div class="item">a</div>
          <div class="item">b</div>
        </div>
        <div class="platform">
          <div class="item">c</div>
          <div class="item">d</div>
        </div>
      </div>
    </div>