xxxxxxxxxx
1
<div class="masonry-with-columns" id="masonry-with-columns">
2
<div style="order: 0;">
3
1
4
</div>
5
<div style="order: 1;">
6
2
7
</div>
8
<div style="order: 2;">
9
3
10
</div>
11
<div style="order: 0;">
12
4
13
</div>
14
<div style="order: 1;">
15
5
16
</div>
17
<div style="order: 2;">
18
6
19
</div>
20
<div style="order: 0;">
21
7
22
</div>
23
<div style="order: 1;">
24
8
25
</div>
26
<div style="order: 2;">
27
9
28
</div>
29
<div style="order: 0;">
30
10
31
</div>
32
<div style="order: 1;">
33
11
34
</div>
35
<div style="order: 2;">
36
12
37
</div>
38
<div style="order: 0;">
39
13
40
</div>
41
<div style="order: 1;">
42
14
43
</div>
44
<div style="order: 2;">
45
15
46
</div>
47
</div>
xxxxxxxxxx
1
body {
2
margin: 0;
3
padding: 1rem;
4
}
5
.masonry-with-columns {
6
display: -webkit-box;
7
display: -webkit-flex;
8
display: -ms-flexbox;
9
display: flex;
10
-webkit-box-orient: vertical;
11
-webkit-box-direction: normal;
12
-webkit-flex-direction: column;
13
-ms-flex-direction: column;
14
flex-direction: column;
15
-webkit-flex-wrap: wrap;
16
-ms-flex-wrap: wrap;
17
flex-wrap: wrap;
18
max-height: 1000px; }
19
.masonry-with-columns div {
20
-webkit-box-flex: 1;
21
-webkit-flex: 1 0 auto;
22
-ms-flex: 1 0 auto;
23
flex: 1 0 auto;
24
background: #00997B;
25
color: white;
26
margin: 0 1rem 1rem 0;
27
text-align: center;
28
font-weight: 900;
29
font-size: 2rem; }
30
.masonry-with-columns div:nth-child(1) {
31
height: 163px;
32
line-height: 163px; }
33
.masonry-with-columns div:nth-child(2) {
34
height: 495px;
35
line-height: 495px; }
36
.masonry-with-columns div:nth-child(3) {
37
height: 120px;
38
line-height: 120px; }
39
.masonry-with-columns div:nth-child(4) {
40
height: 309px;
41
line-height: 309px; }
42
.masonry-with-columns div:nth-child(5) {
43
height: 317px;
44
line-height: 317px; }
45
.masonry-with-columns div:nth-child(6) {
46
height: 277px;
47
line-height: 277px; }
48
.masonry-with-columns div:nth-child(7) {
49
height: 339px;
50
line-height: 339px; }
51
.masonry-with-columns div:nth-child(8) {
52
height: 491px;
53
line-height: 491px; }
54
.masonry-with-columns div:nth-child(9) {
55
height: 281px;
56
line-height: 281px; }
57
.masonry-with-columns div:nth-child(10) {
58
height: 499px;
59
line-height: 499px; }
60
.masonry-with-columns div:nth-child(11) {
61
height: 490px;
62
line-height: 490px; }
63
.masonry-with-columns div:nth-child(12) {
64
height: 289px;
65
line-height: 289px; }
66
.masonry-with-columns div:nth-child(13) {
67
height: 356px;
68
line-height: 356px; }
69
.masonry-with-columns div:nth-child(14) {
70
height: 406px;
71
line-height: 406px; }
72
.masonry-with-columns div:nth-child(15) {
73
height: 196px;
74
line-height: 196px; }
75
.masonry-with-columns div:nth-child(16) {
76
height: 237px;
77
line-height: 237px; }
78
.masonry-with-columns div:nth-child(17) {
79
height: 367px;
80
line-height: 367px; }
81
.masonry-with-columns div:nth-child(18) {
82
height: 472px;
83
line-height: 472px; }
84
.masonry-with-columns div:nth-child(19) {
85
height: 413px;
86
line-height: 413px; }
87
.masonry-with-columns div:nth-child(20) {
88
height: 337px;
89
line-height: 337px; }
90
.masonry-with-columns div:nth-child(21) {
91
height: 406px;
92
line-height: 406px; }
93
.masonry-with-columns div:nth-child(22) {
94
height: 368px;
95
line-height: 368px; }
96
.masonry-with-columns div:nth-child(23) {
97
height: 130px;
98
line-height: 130px; }
99
.masonry-with-columns div:nth-child(24) {
100
height: 209px;
101
line-height: 209px; }
102
.masonry-with-columns div:nth-child(25) {
103
height: 203px;
104
line-height: 203px; }
105
.masonry-with-columns div:nth-child(26) {
106
height: 368px;
107
line-height: 368px; }
108
.masonry-with-columns div:nth-child(27) {
109
height: 163px;
110
line-height: 163px; }
111
.masonry-with-columns div:nth-child(28) {
112
height: 418px;
113
line-height: 418px; }
114
.masonry-with-columns div:nth-child(29) {
115
height: 123px;
116
line-height: 123px; }
117
.masonry-with-columns div:nth-child(30) {
118
height: 256px;
119
line-height: 256px; }
120
.masonry-with-columns div:nth-child(31) {
121
height: 196px;
122
line-height: 196px; }
123
.masonry-with-columns div:nth-child(32) {
124
height: 224px;
125
line-height: 224px; }
126
.masonry-with-columns div:nth-child(33) {
127
height: 122px;
128
line-height: 122px; }
129
.masonry-with-columns div:nth-child(34) {
130
height: 303px;
131
line-height: 303px; }
132
.masonry-with-columns div:nth-child(35) {
133
height: 159px;
134
line-height: 159px; }
135
.masonry-with-columns div:nth-child(36) {
136
height: 320px;
137
line-height: 320px; }
xxxxxxxxxx
1
const numCols = 3;
2
const colHeights = Array(numCols).fill(0);
3
const container = document.getElementById('masonry-with-columns');
4
Array.from(container.children).forEach((child, i) => {
5
const order = i % numCols;
6
child.style.order = order;
7
colHeights[order] += parseFloat(child.clientHeight);
8
})
9
container.style.height = Math.max(...colHeights) + 'px';
Console errors: 0