I want to build this layout so my images, that are in 2nd and 3rd row take remaining space, to go all the way to first row. I just displayed flex and set flex-wrap: wrap. But that does not solve my problem. This is what i want
Asked
Active
Viewed 45 times
0

Temani Afif
- 245,468
- 26
- 309
- 415

JEKO10
- 27
- 1
- 3
-
It's more close to the grid layout rather than the flexbox. use grid. – BehRouz Sep 12 '22 at 19:12
-
are these random images or are they always the same ratio ? – G-Cyrillus Sep 12 '22 at 19:13
-
You could use `flex-direction: column`. – Azu Sep 12 '22 at 19:56
-
masonry is probably not needed if you already know the size/ratio of your images. they can be spanning through rows and be placed into columns. example https://codepen.io/gc-nomade/pen/xxjEEOJ – G-Cyrillus Sep 13 '22 at 19:24
1 Answers
0
use column-count for the container of images check here for more info
function getRandomSize(min, max) {
return Math.round(Math.random() * (max - min) + min);
}
var allImages = "";
for (var i = 0; i < 30; i++) {
var width = getRandomSize(200, 400);
var height = getRandomSize(200, 400);
allImages += '<img src="https://placekitten.com/'+width+'/'+height+'?random='+i+'" alt="gallery photo">';
}
$('#photos').append(allImages);
#photos {
/* Prevent vertical gaps */
line-height: 0;
-webkit-column-count: 5;
-webkit-column-gap: 0px;
-moz-column-count: 5;
-moz-column-gap: 0px;
column-count: 5;
column-gap: 0px;
}
#photos img {
/* Just in case there are inline attributes */
width: 100% !important;
height: auto !important;
}
@media (max-width: 1200px) {
#photos {
-moz-column-count: 4;
-webkit-column-count: 4;
column-count: 4;
}
}
@media (max-width: 1000px) {
#photos {
-moz-column-count: 3;
-webkit-column-count: 3;
column-count: 3;
}
}
@media (max-width: 800px) {
#photos {
-moz-column-count: 2;
-webkit-column-count: 2;
column-count: 2;
}
}
@media (max-width: 400px) {
#photos {
-moz-column-count: 1;
-webkit-column-count: 1;
column-count: 1;
}
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<section id="photos"></section>

Mohammad
- 36
- 3