I want to display a dynamic list of elements with different heights from left to right. Wrapping rows elements should start right under the above rows element bottom, eliminating any space for the grid (pulling them to above element).
Desired outcome:
I have tried to use both flex and grid, but it always creates an even row with the height of the tallest element.
*,
body {
margin: 0;
padding: 0;
}
main {
display: grid;
grid-template-columns: repeat(4, auto);
gap: 1rem;
width: 100vw;
}
.box {
background-color: red;
padding: 1rem;
}
.short {
height: 5rem;
}
.middle {
height: 8rem;
}
.tall {
height: 12rem;
}
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="style.css">
<title>FlexBox</title>
</head>
<body>
<main>
<div class="box tall">1</div>
<div class="box short">2</div>
<div class="box middle">3</div>
<div class="box short">4</div>
<div class="box tall">5</div>
<div class="box short">6</div>
<div class="box middle">7</div>
<div class="box short">8</div>
<div class="box tall">9</div>
<div class="box short">10</div>
<div class="box middle">11</div>
<div class="box short">12</div>
</main>
</body>
</html>
Is there any simpler solution for this that I'm missing?