LoginSignup
2
1

More than 1 year has passed since last update.

【CSS】Youtube動画の埋め込みにpadding-topは使わない時代

Posted at

「なんでYoutubeの埋め込みごときに、padding-top でダラダラと書かないといけないの?」と思ったことありませんか?

僕はずっと思ってました( ; ; )

今回はそれを覆すプロパティを紹介します。

従来のYoutube動画の埋め込み方法(padding-top)

これまでは、iframeに親要素を作り、padding-top: 56.25%; or padding-top: calc(100% * 16 / 9); を指定することで、iframeposition: absolute; で絶対配置にしていました。

<div class="wrapper">
  <iframe src="動画URL" ></iframe>
</div>
.wrapper {
  position: relative;
  padding-top: 56.25%;
}

iframe {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

padding-topは使わず16:9でYoutube動画を埋め込む方法(aspect-ratio)

2021年頃あたり?から aspect-ratio を使うだけで、16:9のアスペクト比で固定されます。

iframe {
  aspect-ratio: 16/9;
  width: 100%;
  height: 100%;
}

そして気になるのが、対応ブラウザですよね。

aspect-ratioの対応ブラウザ

aspect-ratio は現在、全モダンブラウザ(最新版)で使えます。

Can I use - aspect-ratio

スクリーンショット 2023-01-16 8.53.45.png

これが新時代ですね💡

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