方法-レスポンシブIframe
CSSを使用してレスポンシブiframeを作成する方法を学びます。
レスポンシブIframe
サイズ変更時にアスペクト比(4:3、16:9など)を維持するiframeを作成します。
アスペクト比とは何ですか?
要素のアスペクト比は、要素の幅と高さの比例関係を表します。2つの一般的なビデオアスペクト比は、4:3(20世紀のユニバーサルビデオフォーマット)と16:9(HDテレビとヨーロッパのデジタルテレビ、およびYouTubeビデオのユニバーサル)です。
方法-レスポンシブIframe
ステップ1)HTMLを追加します。
<div>などのコンテナ要素を使用し、その中にiframeを追加します。
例
<div class="container">
<iframe
class="responsive-iframe"
src="https://www.youtube.com/embed/tgbNymZ7vqY"></iframe>
</div>
ステップ2)CSSを追加します。
padding-top
コンテナDIVのアスペクト比を維持するためのパーセンテージ値を追加します。次の例では、16:9のアスペクト比を作成します。これは、YouTubeビデオのデフォルトのアスペクト比です。
例16:9アスペクト比
.container {
position: relative;
overflow: hidden;
width: 100%;
padding-top: 56.25%; /* 16:9 Aspect
Ratio (divide 9 by 16 = 0.5625) */
}
/* Then style the iframe to fit in the container
div with full height and width */
.responsive-iframe {
position: absolute;
top: 0;
left: 0;
bottom: 0;
right: 0;
width: 100%;
height: 100%;
}
その他のアスペクト比:
例4:3アスペクト比
.container {
padding-top: 75%; /*
4:3 Aspect
Ratio */
}
例3:2アスペクト比
.container {
padding-top: 66.66%; /*
3:2 Aspect
Ratio */
}
例8:5アスペクト比
.container {
padding-top: 62.5%; /*
8:5 Aspect
Ratio */
}
例1:1のアスペクト比(高さと幅は常に等しい)
.container {
padding-top: 100%; /*
1:1 Aspect
Ratio */
}