0

I have divs of different heights. I want to place these in a grids without any blank white space. Please follow below.codesandbox.

.container {
      display: flex;
      flex-wrap: wrap;
      width: 400px;
    } 
    
    .sm {
      height: 200px;
      background: gold;
      border: 1px solid black;
      width: 45%; 
    }
    
    .md {
      height: 250px;
      background: red;
      border: 1px solid black;
      width: 45%;
    }
   <div class="container">
            <div class="sm"></div>
            <div class="md"></div>
            <div class="sm"></div>
            <div class="sm"></div>
            <div class="md"></div>
            <div class="sm"></div>
            <div class="sm"></div>
            <div class="md"></div>
        </div>
Shubham Srivastava
  • 1,807
  • 1
  • 10
  • 17
Sujoy Saha
  • 220
  • 1
  • 13
  • You could try [columns](https://developer.mozilla.org/en-US/docs/Web/CSS/columns) instead of flex. – Yoshi Sep 21 '20 at 09:25

2 Answers2

0

You can not remove blank white space when different box has a different height. To remove vertical blank space use a similar height for all the boxes.

You used margin for .container *. That sets margin for all the elements inside the container. To remove horizontal white space, do not use margin for .container *.

Kawsar Hamid
  • 514
  • 3
  • 13
0

You are not able to remove whitespace in one div if the height is different in each section. So it is needed to divide each section as follows.

.container {
  display: flex;
  width: 400px;
}

.items {
  display: flex;
  flex-direction: column;
  width: 45%;
}

.sm {
  height: 200px;
  background: gold;
  border: 1px solid black;
  width: 100%; 
}

.md {
  height: 250px;
  background: red;
  border: 1px solid black;
  width: 100%;
}
<div class="container">
  <div class="items">
    <div class="sm"></div>
    <div class="sm"></div>
    <div class="md"></div>
    <div class="sm"></div>
  </div>
  <div class="items">
    <div class="md"></div>
    <div class="sm"></div>
    <div class="sm"></div>
    <div class="md"></div>
  </div>
</div>
Derek Wang
  • 10,098
  • 4
  • 18
  • 39