2

In the code box-2 and box-3 row height auto stretches to box-1 height which is tallest item in the row. There is a extra gap in box-2 and box-3 columns. I want that gap to be filled up by the box-5 which is in the second row. Fiddle

.container {
  display: grid;
  grid-template-columns: 20% 40% 40%;
  grid-gap: 20px;
}

.container > div {
  border: 1px solid red;
}

.box-1 {
  background-color: lightgreen;
  height: 300px;
}

.box-2 {
  background-color: lightsalmon;
  height: 150px;
}

.box-3 {
  background-color: lightsalmon;
  height: 150px;
}

.box-4 {
  background-color: lightskyblue;
  height: 500px;
}

.box-5 {
  background-color: lightseagreen;
  grid-column: 2/-1;
}
<div class="container">
  <div class="box-1"></div>
  <div class="box-2"></div>
  <div class="box-3"></div>
  <div class="box-4"></div>
  <div class="box-5"></div>
</div>

This is the output I am looking for

This is the output I am looking for

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
am guru prasath
  • 345
  • 1
  • 7
  • 28
  • This is a possible duplicate the similar problem is solved here https://stackoverflow.com/questions/45791809/different-height-of-css-grid-cells – Manas Apr 11 '20 at 15:01

2 Answers2

1

You're setting the height of the grid items directly, and not defining any rows on the grid container. Therefore, the grid algorithm has to create rows to accommodate the grid areas. It only needs to create two rows to complete the layout. That's why there's a large gap beneath boxes 2 and 3. Box 1, being the tallest, sets the height of top row.

The layout you want requires at least three rows.

Try this approach: Set the rows (and heights) at the container level, then set the grid areas.

.container {
  display: grid;
  grid-template-columns: 1fr 2fr 2fr;
  grid-template-rows: repeat(3, 150px);
  grid-gap: 10px;
}

.box-1 {
  grid-column: 1;
  grid-row: 1 / span 2;
  background-color: lightgreen;
}

.box-2 {
  grid-column: 2;
  grid-row: 1;
  background-color: lightsalmon;
}

.box-3 {
  grid-column: 3;
  grid-row: 1;
  background-color: lightsalmon;
}

.box-4 {
  grid-column: 2 / -1;
  grid-row: 2 / 4;
  background-color: lightskyblue;
}

.box-5 {
  grid-column: 1;
  grid-row: 3;
  background-color: lightseagreen;
}
<div class="container">
  <div class="box-1">1</div>
  <div class="box-2">2</div>
  <div class="box-3">3</div>
  <div class="box-4">4</div>
  <div class="box-5">5</div>
</div>

If you want more options for sizing grid areas, then increase the number of rows / columns.

For example, instead of this:

grid-template-rows: repeat(3, 150px)

...you can do this:

grid-template-rows: repeat(9, 50px)

... then span grid areas across rows as needed.

Michael Benjamin
  • 346,931
  • 104
  • 581
  • 701
-1

Keep box 1 and box 3 in one div align it vertically and keep all other 3 boxes in one container in 1 container then u can do this easily