回答編集履歴

2

コード追記

2022/06/21 04:50

投稿

hatena19
hatena19

スコア33782

test CHANGED
@@ -18,3 +18,24 @@
18
18
  position: absolute; /* 追加 */
19
19
  }
20
20
  ```
21
+
22
+ おまけ
23
+ ---
24
+ CSS の aspect-ratio を使うと簡単に正方形にできます。
25
+
26
+ [aspect-ratio - CSS: カスケーディングスタイルシート | MDN](https://developer.mozilla.org/ja/docs/Web/CSS/aspect-ratio)
27
+
28
+ IEは非対応ですが、すでにサポート終了してますので無視していいでしょう。
29
+
30
+ ```css
31
+ div.container div {
32
+ width: calc(100% / 3);
33
+ aspect-ratio: 1 / 1;
34
+ }
35
+
36
+ div.container div img {
37
+ width: 100%;
38
+ height: 100%;
39
+ object-fit: cover;
40
+ }
41
+ ```

1

コードにコメント追加

2022/06/20 08:09

投稿

hatena19
hatena19

スコア33782

test CHANGED
@@ -8,13 +8,13 @@
8
8
  width: calc(100% / 3);
9
9
  height: 0;
10
10
  padding-bottom: calc(100% / 3);
11
- position: relative;
11
+ position: relative; /* 追加 */
12
12
  }
13
13
 
14
14
  div.container div img {
15
15
  width: 100%;
16
16
  height: 100%;
17
17
  object-fit: cover;
18
- position: absolute;
18
+ position: absolute; /* 追加 */
19
19
  }
20
20
  ```