4

I have a list of items I want to display with CSS. Originally, it was only two items side-by-side on one line but now I want to make it responsive for larger screens so I want to make it display 3 items on one line instead. My old code looks like this with justify-content:space-between. It looks good with an odd number of items to display.

.flex-container-old{
  margin-top: 50px;
  background: magenta;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.box-old{
  width: 40%;
  border: 1px solid black;
  margin-bottom: 20px;
  height: 300px;
  background: orange;
}

.wrapper{
  margin: 0 auto;
  width: 80%;
}

body{
background:#D3D3D3;
}
<div class="wrapper">
      <div class="flex-container-old">
          <div class="box-old">
          </div>
          <div class="box-old">
          </div>
          <div class="box-old">
          </div>
          <div class="box-old">
          </div>
          <div class="box-old">
          </div>
      </div>
  </div>

So naturally I extended it to three items in one row by modifying the width property only to end up with the below.

.flex-container-new{
  background: lightblue;
  display: flex;
  flex-wrap: wrap;
  justify-content: space-between;
}
.box {
  width: 30%;
  border: 1px solid black;
  margin-bottom: 20px;
  height: 300px;
  background: orange;
}

.wrapper{
  margin: 0 auto;
  width: 80%;
}
<div class="wrapper">
    <div class="flex-container-new">
      <div class="box">
      </div>
      <div class="box">
      </div>
      <div class="box">
      </div>
      <div class="box">
      </div>
      <div class="box">
      </div>
    </div>
</div>

My problem in the case of the above code with three items on one line is I want the last item in last row to be pushed to the left, aligned with the middle item in the row above it. Sadly bootstrap is not an option. This is for learning purposes. It there a way I can achieve the above with just CSS? Many thanks in advance.

Pirsanth
  • 68
  • 1
  • 7

1 Answers1

3

This is easier to control using CSS Grid because we can dictate both the x and y axis. With Flexbox, you can only reliably control the x axis. If you haven't heard about the fr unit, it's defined by Mozilla as follows:

The fr, which is short for “fraction”, is a unit which represents a fraction of the available space in the grid container.

Another nice thing about using Grid is that we can drop the height and margin-bottom set in .box and also the flex-wrap rule. Everything about the layout of this grid, from the height of the cells to the grid-gap spacing between them, is all defined in the parent.

.grid-container-new {
  background: lightblue;
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  grid-template-rows: repeat(2, 300px);
  grid-gap: 20px;
}

.box {
  border: 1px solid black;
  background: orange;
}
<div class="grid-container-new">
  <div class="box">
  </div>
  <div class="box">
  </div>
  <div class="box">
  </div>
  <div class="box">
  </div>
  <div class="box">
  </div>
</div>

jsFiddle

Andy Hoffman
  • 18,436
  • 4
  • 42
  • 61