I'm working on a photo album that loads several photos with different height and need to stack them at the top of the container, like this:
To do so, I settled the display property of the container to flex with align-items: flex-start and the children to inline-flex, like this:
HTML
<div class="album1">
<div>1</div>
<div>2</div>
<div>3</div>
<div>4</div>
<div>5</div>
<div>6</div>
<div>7</div>
<div>8</div>
<div>9</div>
</div>
SCSS
.album1 {
display: flex;
flex-wrap: wrap;
align-items: flex-start;
div {
display: inline-flex;
width: calc(100% / 3);
height: 100px;
background-color: red;
box-sizing: border-box;
border: 1px solid black;
justify-content: center;
align-items: center;
&:nth-child(3n+2) {
height: 200px;
}
}
}
(Note that I set the height of the divs just for the example)
The behavior I got is this one.
I'm actually looking for a pure CSS solution, but I could go with a JS solution too.