NewbCake
NewbCake

Reputation: 443

CSS Grid responsive layout with auto-fit and minmax for even number of items

What I am trying to accomplish:

There are 4 grid items. At the a widest screen size, I would like the items to be lined up in a row:

item_1 item_2 item_3 item_4

As the screen width shrinks, I would like the items to wrap to the next row like this:

item_1 item_2

item_3 item_4

Then finally at the narrowest, I would like the times to be a single columns

item_1

item_2

item_3

item_4

I found an example which does this but by only wrapping the next item when it can – https://labs.jensimmons.com/2017/03-009.html

Building off of that I tried using nested grid containers based on the example's model:

grid-template-columns: repeat(auto-fit, minmax(600px, 1fr));

HTML

  .outer_grid {
      display:grid;
      grid-template-columns: repeat(auto-fit, minmax(600px, 1fr));
      grid-template-row:1fr 1fr;
      grid-gap:1em;
      border:5px solid blue;
    }

    .grid {
      display:grid;
      grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
      grid-template-row:1fr 1fr;
      grid-gap:1em;
      border:5px solid green;
      height:200px;
    }

    .item {
      border:2px solid red;
    }
    <div class="outer_grid">
      <div class="grid">
        <div class="item">item 1</div>
        <div class="item">item 2</div>
      </div>
      <div class="grid">
        <div class="item">item 3</div>
        <div class="item">item 4</div>
      </div>
    </div>

It is nearly working (codepen below) but I'm not sure if this is the right approach or if there is a better way to accomplish this. I have tried using Flexbox too but chose CSS Grid because of the grid-gap feature. Also, I know it can be done but I am trying to do this without media queries.

https://codepen.io/anon/pen/LMqWEr?editors=1100

Upvotes: 3

Views: 2676

Answers (2)

Ron
Ron

Reputation: 31

I tried the below fix for this question and hope it works

https://codepen.io/ronsummer/pen/eYOKRzg

<div class="wrapper">
  <div class="box a">A</div>
  <div class="box b">B</div>
  <div class="box c">C</div>
  <div class="box d">D</div>
  <div class="box e">E</div>
  <div class="box f">F</div>
</div>

body {
  margin: 40px;
}

.wrapper {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  grid-gap: 10px;
  background-color: #fff;
  color: #444;
}

.box {
  background-color: #444;
  color: #fff;
  border-radius: 5px;
  padding: 20px;
  font-size: 150%;
}

Upvotes: 3

Alexandru Popa
Alexandru Popa

Reputation: 11

So from my understanding, you are actually looking for a row of items to be transformed into a column or multiple columns of items when shrinking, without using media queries.

Taking this into consideration, the best approach would be to use Flexbox since it's 1 dimensional.

CSS Grid is also powerful but only when you want to make use of 2-dimensional layout (so both rows & columns).

Also is good to know that Flexbox is content-first opposed to CSS Grid which is layout-first.

Codepen

.flex-container {
    display: flex;
    width: 100%;
    flex-wrap: wrap;
    justify-content: center;
}

.flex-container .flex-item {
    display: flex;
    min-width: 10rem;
    width: calc((100% - 1rem * 8 * 2) / 8);
    height: 10rem;
    padding: 1rem;
    margin: 1rem;
    align-items: center;
    justify-content: center;
    background: #1d1d1d;
    color: #fff;
    font-size: 2rem;
    font-weight: 500;
    text-align: center;
}
<div class="flex-container">
    <div class="flex-item">1</div>
    <div class="flex-item">2</div>
    <div class="flex-item">3</div>
    <div class="flex-item">4</div>
    <div class="flex-item">5</div>
    <div class="flex-item">6</div>
    <div class="flex-item">7</div>
    <div class="flex-item">8</div>
</div>

Upvotes: 1

Related Questions