youtube埋め込み動画をレスポンシブ対応にする

html

<div class="video-container">
  <iframe src="【Youtube埋め込み用URL】" frameborder="0" allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
</div>

CSS(SCSS)


.video-container {
  position: relative;
  width: 100%;
  padding-top: 56.25%;
  iframe {
    position: absolute;
    top: 0;
    right: 0;
    width: 100% !important;
    height: 100% !important;
  }
}

解説

padding-topで動画のアスペクト比を取得しています。 youtubeは16:9なので、100 / 16 * 9 = 56.25% となります。

参考URL: iframe要素(Youtubeなど)のレスポンシブ対応