1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

AlphabetAdvent Calendar 2022

Day 1

【A】aspect-ratioで縦横比を指定する

Last updated at Posted at 2022-11-30

経緯

『この画像縦横1:1で表示させたいけど%だとwidthに合わせてheight指定するとかできないんだよな〜』

仕様

「heightやwidthがautoのときに参考にする縦横比の値」だそうです。

実装例

webページのトップにあるみたいな横幅いっぱいの画像を作る

.mainVisual {
    width: 100vw; /* widthかheightを片方指定した上で、もう片方はautoとかに。 */
    height: auto;
    aspect-ratio: 5 / 2; /* "横/縦"。数字1つのみを指定する場合は縦=1としたときの横。 */
}

その他、Youtubeの埋め込み(iframe)を囲うdivにaspect-ratio: 16/9;を指定しながらwidth: 100%をするとレスポンシブ対応しやすいはず。

1
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
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?