1

I've seen several questions similar to this on SO, but I haven't been able to make them work. So, here's a simple example:

enter image description here

The code:

<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <style>
    body {
      margin: 0;
      padding: 0;
      font-family: Arial, sans-serif;
    }

    .container {
      display: flex;
      flex-wrap: wrap;
    }

    .section {
      box-sizing: border-box;
      width: 100%;
      padding: 20px;
    }

    .section-1 {
      background-color: #f1c40f;
      height: 100px;
    }

    .section-2 {
      background-color: #2ecc71;
      height: 200px;
    }

    .section-3 {
      background-color: #3498db;
      height: 300px;
    }

    @media (min-width: 768px) {
      .section {
        width: 50%;
      }
    }
  </style>
</head>
<body>
  <div class="container">
    <div class="section section-1">
      Section 1 - Height: 100px
    </div>
    <div class="section section-2">
      Section 2 - Height: 200px
    </div>
    <div class="section section-3">
      Section 3 - Height: 300px
    </div>
  </div>
</body>
</html>

See there's an empty space under Section 1. But I want Section 3 to be immediately under it so they fit nicely like a puzzle.

What am I missing?

Thank you!

pbns
  • 63
  • 1
  • 1
  • 4

1 Answers1

0

The layout type you want to achieve is called "Masonry Layout". Here you can read about the ways you can implement it with flexbox.

Fuad
  • 350
  • 10