回答編集履歴

2

説明の改善

2020/10/16 13:58

投稿

hatena19
hatena19

スコア33782

test CHANGED
@@ -24,7 +24,7 @@
24
24
 
25
25
  時間があったので**CSS GridでMasonry的なものを作成**してみました。
26
26
 
27
- 質問の[実コード](https://jsfiddle.net/cb4dk6av/)を元にしました。
27
+ 質問の[実コード](https://jsfiddle.net/cb4dk6av/)を元にしました。
28
28
 
29
29
 
30
30
 
@@ -125,3 +125,7 @@
125
125
  </div>
126
126
 
127
127
  ```
128
+
129
+
130
+
131
+ rs2 rs3 ・・・のクラスの設定はJavaScriptを使って各ブロックの高さを取得して設定するようにすれば手書きでちまちまする必要はなくなりますね。

1

コード追記

2020/10/16 13:58

投稿

hatena19
hatena19

スコア33782

test CHANGED
@@ -1,6 +1,6 @@
1
1
  まず、`flex: 0.5fr;` の部分ですが、fr は CSS Grid の単位で、Flexboxでは使えなかったと思います。
2
2
 
3
- `flex: 0 0 50%;` としていすべきでしょう。
3
+ `flex: 0 0 50%;` と指定すべきでしょう。
4
4
 
5
5
 
6
6
 
@@ -15,3 +15,113 @@
15
15
 
16
16
 
17
17
  高さも幅も異なる要素がある場合は、CSS Grid を使うことになるでしょう。
18
+
19
+
20
+
21
+ 追記
22
+
23
+ ---
24
+
25
+ 時間があったので**CSS GridでMasonry的なものを作成**してみました。
26
+
27
+ 質問の[実行コード](https://jsfiddle.net/cb4dk6av/)を元にしました。
28
+
29
+
30
+
31
+ [CodePenサンプル](https://codepen.io/hatena19/pen/GRqZrqg)
32
+
33
+
34
+
35
+ CSSのポイント部分は下記になります。
36
+
37
+ Gridの自動配置を最大限利用してます。(IE11には非対応です。)
38
+
39
+
40
+
41
+ ```css
42
+
43
+ .product-are-container {
44
+
45
+ display: grid;
46
+
47
+ justify-content: center; /* 左右中央寄せ */
48
+
49
+ grid-template-columns: repeat(auto-fill, 25rem); /* 横幅に合わせて繰り返す */
50
+
51
+ grid-auto-rows: 3.2rem; /* 行を必用なだけ繰り返す */
52
+
53
+ grid-auto-flow: dense; /* なるべく隙間なく敷き詰める */
54
+
55
+ gap: 0.5rem;
56
+
57
+ margin: 0.5rem;
58
+
59
+ }
60
+
61
+
62
+
63
+ /* 以下各ブロックが何行分占有するかを指定 */
64
+
65
+ .rs2 {
66
+
67
+ grid-row: span 2;
68
+
69
+ }
70
+
71
+ .rs3 {
72
+
73
+ grid-row: span 3;
74
+
75
+ }
76
+
77
+ .rs4 {
78
+
79
+ grid-row: span 4;
80
+
81
+ }
82
+
83
+ .rs5 {
84
+
85
+ grid-row: span 5;
86
+
87
+ }
88
+
89
+ .rs5 {
90
+
91
+ grid-row: span 6;
92
+
93
+ }
94
+
95
+ ```
96
+
97
+
98
+
99
+ HTMLではそのブロックが何行分の高さかを rs2 rs3 ・・・のクラスを設定することで指定します。
100
+
101
+
102
+
103
+ ```html
104
+
105
+ <div class="product-are-container">
106
+
107
+ <div class="category-wrapper rs2">
108
+
109
+ <!-- 2行分の高さ -->
110
+
111
+ </div>
112
+
113
+ <div class="category-wrapper rs3">
114
+
115
+ <!-- 3行分の高さ -->
116
+
117
+ </div>
118
+
119
+ <div class="category-wrapper rs4">
120
+
121
+ <!-- 4行分の高さ -->
122
+
123
+ </div>
124
+
125
+ </div>
126
+
127
+ ```