htmlcss Youtube (iframe) をレスポンシブ対応に

2014年2月25日

Youtube(iframe 要素)をレスポンシブに対応させる方法のメモ。

概要

iframe で出力された Youtube 動画を CSS を使ってレスポンシブに対応させる。

参考にしたサイト:「Thierry Koblentz / A List Apart : Creating Intrinsic Ratios for Video

サンプル

Youtube から iframe の埋め込みコード(共有 → 埋め込みコード)を取得して、それを任意のクラス名を付けた div 要素(class=”youtube”)で囲む。

  1. <div class="youtube">
  2. <iframe width="640" height="360" src="//www.youtube.com/embed/dH3GSrCmzC8" frameborder="0" allowfullscreen></iframe>
  3. </div><!--end of .youtube-->

以下のように CSS を指定する。

  1. .youtube {
  2. position: relative;
  3. padding-bottom: 56.25%;
  4. padding-top: 25px;
  5. height: 0;
  6. overflow: hidden;
  7. }
  8.  
  9. .youtube iframe {
  10. position: absolute;
  11. top: 0;
  12. left: 0;
  13. width: 100%;
  14. height: 100%;
  15. }

.youtube

position: relative
子要素の基準とする

padding-bottom: 56.25%
幅の 56.25% を高さとする設定(16:9 の場合 → 9 ÷ 6 = 56.25 )

padding-top: 25px
クロム用の高さを指定

height: 0
古い IE のレイアウト用の指定

.youtube iframe

position: absolute
親要素(.youtube)のパディング領域に配置するために、絶対配置を指定

top: 0
トップに配置

left: 0
左に配置

width: 100%
親コンテナの幅いっぱいに表示

height: 100%
親コンテナの高さいっぱいに表示

レイアウト用の div 要素を追加

レイアウト用の div 要素(class=”yt_wrapper”)を追加して、最大幅、最小幅やレイアウト等を追加

  1. <div class="yt_wrapper">
  2. <div class="youtube">
  3. <iframe width="640" height="360" src="//www.youtube.com/embed/dH3GSrCmzC8" frameborder="0" allowfullscreen></iframe>
  4. </div><!--end of .youtube-->
  5. </div><!--end of .yt_wrapper-->

追加した CSS

  1. .yt_wrapper {
  2. max-width: 640px;
  3. min-width: 280px;
  4. margin: 20px auto;
  5. border: 1px solid #CCC;
  6. }

異なる縦横比やクロムの高さのクラスの追加

異なる縦横比やクロムの高さが指定できるように以下のようなクラスを追加。

サンプル

  • .fourBYthree:動画を「4:3」で表示
  • .chrome_35 :クロムの高さを「35px」で表示

デフォルトでは動画を「16:9」で、クロムの高さを「25px」で表示し、これらのクラスを指定することで、縦横比やクロムの高さを変更可能になる。(但し、クロムの高さは実際にには固定のようで、クロムの高さを変更するとコンテンツの高さが増えるみたい。。。)

HTML

  1. <!--デフォルト「16:9」クロムの高さを「25px」-->
  2. <div class="yt_wrapper">
  3. <div class="youtube">
  4. <iframe width="640" height="360" src="//www.youtube.com/embed/dH3GSrCmzC8" frameborder="0" allowfullscreen></iframe>
  5. </div><!--end of .youtube-->
  6. </div><!--end of .yt_wrapper-->
  7.  
  8. <!--「4:3」クロムの高さを「35px」-->
  9. <div class="yt_wrapper">
  10. <div class="youtube fourBYthree chrome_35">
  11. <iframe width="640" height="360" src="//www.youtube.com/embed/dH3GSrCmzC8" frameborder="0" allowfullscreen></iframe>
  12. </div><!--end of .youtube-->
  13. </div><!--end of .yt_wrapper-->

追加したCSS

  1. .fourBYthree {
  2. padding-bottom: 75%; //3 ÷ 4 = 75
  3. }
  4.  
  5. .chrome_35 {
  6. padding-top: 35px;
  7. }

画面サイズにより表示・非表示

複数の Youtube 動画を表示する場合、数が多いとスマートフォンなどではロードするのに時間がかかりすぎてしまうので、画面サイズが小さい場合は Youtube へのリンクのみを表示する例。(この方法が実用的かどうかは別)

サンプル

HTML では、画面が小さい場合のリンクを記述しておき、jQuery で画面サイズが大きければ要素を書き換える。

HTML

  1. <div class="yt_wrapper">
  2. <h3><a href="https://www.youtube.com/watch?v=dH3GSrCmzC8" target="_blank">Bill Evans - Waltz For Debby </a></h3>
  3. </div><!--end of .yt_wrapper-->
  4.  
  5. <div class="yt_wrapper">
  6. <h3><a href="https://www.youtube.com/watch?v=qfGHxzKeHvM" target="_blank">Bill Evans Trio - My Foolish Heart </a></h3>
  7. </div><!--end of .yt_wrapper-->
  8. <div class="yt_wrapper">
  9. <h3><a href="https://www.youtube.com/watch?v=csGnvbJCQKU" target="_blank">Bill Evans Trio - Nardis</a></h3>
  10. </div><!--end of .yt_wrapper-->

jQuery では画面幅が 600px 以上の場合に以下を行う。

  • div 要素(class=”yt_wrapper”)に対して「each()」で a 要素の href 属性から iframe で使用する src 属性に使う値を取得して src 属性を作成
  • iframe を作成して src 属性を指定し div 要素(class=”youtube”)で囲んで追加(append)する
  • リンク(a 要素)を削除

jQuery

  1. <script src="http://code.jquery.com/jquery-1.10.1.min.js"></script>
  2. <script>
  3. jQuery(function($){
  4. function replace_yt() {
  5. $('.yt_wrapper').each(function(index, element) {
  6. var this$ = $(this);
  7. var yt_src = this$.find('a').attr('href').replace(/http(s)?:\/\/www.youtube.com\/watch\?v=/, "");
  8. yt_src = "http://www.youtube.com/embed/" + yt_src;
  9. var html = '<div class="youtube"><iframe src="' + yt_src + '" frameborder="0" allowfullscreen></iframe></div>';
  10. this$.append(html);
  11. var h3_html = this$.find('h3').html();
  12. h3_html = h3_html.replace(/<a href=&#91;^>]+>/, "");
  13. h3_html = h3_html.replace(/<\/a>/, "");
  14. this$.find('h3').html(h3_html);
  15. });
  16. }
  17. if($(window).width() > 600) {
  18. if($('.yt_wrapper').length > 0){
  19. replace_yt();
  20. }
  21. }
  22. var timer = false;
  23. $(window).resize(function(){
  24. var w = $(window).width();
  25. if (timer !== false) {
  26. clearTimeout(timer);
  27. }
  28. timer = setTimeout(function() {
  29. if(w > 600) {
  30. if($('.yt_wrapper').length > 0 && $('.youtube iframe').length === 0){
  31. replace_yt();
  32. }
  33. }
  34.  
  35. }, 200);
  36. });
  37. });
  38. </script>

メディアクエリを追加。

Webkit (SafariやChrome) のメディアクエリー実装にはスクロールバーの幅(15px~20px)を含めないというバグがある。

Webkit 以外ではスクロールバーの幅(15px~20px)を含めるので、600px ではなく 620px とした。(これが良いかどうかは難しい)

また、大きいサイズから小さいサイズにリサイズした場合、Youtube の表示はなくならないので、.youtube や .youtube iframe の記述はデフォルトの CSS に記述しておいた。

CSS

  1. .yt_wrapper h3 {
  2. color: #999;
  3. font-size: 14px;
  4. padding-top: 10px;
  5. padding-left: 20px;
  6. }
  7.  
  8. .yt_wrapper h3 a {
  9. text-decoration: none;
  10. color: #EF9128;
  11. }
  12.  
  13. .yt_wrapper h3 a:hover {
  14. text-decoration: underline;
  15. color: #AB570D;
  16. }
  17. .youtube {
  18. position: relative;
  19. padding-bottom: 56.25%;
  20. padding-top: 25px;
  21. height: 0;
  22. overflow: hidden;
  23. margin: 3%;
  24. }
  25. .youtube iframe {
  26. position: absolute;
  27. top: 0;
  28. left: 0;
  29. width: 100%;
  30. height: 100%;
  31. }
  32.  
  33. @media only screen and (min-width: 620px) {
  34. .yt_wrapper h3 {
  35. color: #777;
  36. font-size: 14px;
  37. text-align: center;
  38. }
  39. .yt_wrapper {
  40. max-width: 700px;
  41. min-width: 280px;
  42. margin: 20px auto;
  43. border: 1px solid #CCC;
  44. }
  45. }