LoginSignup
0
0

More than 1 year has passed since last update.

aspect-ratioでアスペクト比を固定する

Last updated at Posted at 2021-09-12

環境

  • Windows10
  • Chrome93

Chromeでは88から、Firefoxでは89から、Safariは15からです。

aspect-ratio

今回は16:9にします

<div class="container"></div>
.container {
  aspect-ratio: 16 / 9
}

これだけです。とても簡単。

Safariでも使えるようになりました。

未対応ブラウザ

方法1

<div class="container"></div>
.container {
  position: relative
}

#container::before {
  content:"";
  display: block;
  padding-bottom: 56.25%; /*  height(9) / width(16) * 100  */
}

少し使い勝手が悪いです。

方法2

16:9 = w:h
 16h = 9w
   h = 9w / 16

とかいう等式変形で

.container {
  width: 100vw; /* (任意のwidth(100%とかは無理そう)) */
  height: calc(9 * 100vw / 16);
}

こんな感じでいけます。

まとめ

aspect-ratioがおすすめ

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0