Help us understand the problem. What is going on with this article?

[YouTube] iframeのレスポンシブ対応

More than 1 year has passed since last update.

iframeをレスポンシブ対応した時のメモです。
今回はYouTubeを使用例として。

HTML

<div class="frame-wrapper__video">
  <iframe src="https://www.youtube.com/hoge/hogehogehogehogehoge" frameborder="0" allowfullscreen></iframe>
</div>
  • YouTubeから取得したiframeを div で囲う。

CSS

.frame-wrapper__video {
  position: relative;
  width: 100%;
  height: 0;
  padding-bottom: 56.25%;
  overflow: hidden;
  margin-bottom: 50px;
}

.frame-wrapper__video iframe {
  width: 100%;
  height: 100%;
  position: absolute;
  top: 0;
  left: 0;
}
  • 4:3の場合 padding-bottom: 75%;
  • 16:9の場合 padding-bottom: 56.25%;
  • padding-bottompadding-topでも可。

サンプル

表示サンプルは下記で確認できます。

GitHub Pages

GitHubリポジトリ

参考

CSSだけでどんな画像でも4:3や16:9の比率を維持して配置する方法

Why do not you register as a user and use Qiita more conveniently?
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away
Comments
Sign up for free and join this conversation.
If you already have a Qiita account
Why do not you register as a user and use Qiita more conveniently?
You need to log in to use this function. Qiita can be used more conveniently after logging in.
You seem to be reading articles frequently this month. Qiita can be used more conveniently after logging in.
  1. We will deliver articles that match you
    By following users and tags, you can catch up information on technical fields that you are interested in as a whole
  2. you can read useful information later efficiently
    By "stocking" the articles you like, you can search right away