aspect-ratio

見本

説明・補足

aspect-ratioを使ったiframeの埋め込み。
非対応のブラウザ(アップデートされてないiOSのSafariなど…)は従来のpaddingで調整し、対応ブラウザはaspect-ratioで表示されるようにする。
クラス名はお好みで。16/9のみに対応。

HTML

  1. <div class="wrapper">
  2. <div class="iframe_wrap">
  3. <iframe width="560" height="315" src="https://www.youtube.com/embed/Cx2dkpBxst8?rel=0" title="YouTube video player" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" allowfullscreen></iframe>
  4. </div>
  5. </div>

CSS

  1. /* 非対応ブラウザ */
  2. .iframe_wrap {
  3. position: relative;
  4. padding-bottom: 56.25%;
  5. height: 0;
  6. overflow: hidden;
  7. }
  8. .iframe_wrap iframe {
  9. position: absolute;
  10. top: 0;
  11. left: 0;
  12. width: 100%;
  13. height: 100%;
  14. }
  15. @supports (aspect-ratio: 16/9) {
  16. /* 対応ブラウザ */
  17. .iframe_wrap{
  18. aspect-ratio: 16/9;
  19. padding-bottom: initial;
  20. height: auto;
  21. overflow: unset;
  22. }
  23. .iframe_wrap iframe {
  24. position: static;
  25. }
  26. }

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です