html - 我的响应式 youtube 嵌入视频没有填满整个框

标签 html css youtube embed responsive

这之后我搜索了很多。到目前为止,我学会了如何使用绝对定位进行布局。我用 7-8 个盒子制作了我的第一个新布局。其中几个在主箱中。我想在此处用 YouTube 视频填充 1 个框。我认为绝对定位框的所有内容都是响应式的,youtube 视频也是响应式的,但问题是当我将嵌入内容放入我的框时 - 05 没有填满整个框我的意思是它填满了整个宽度但有很多空间在高度选项中。我也尝试更改填充底部,但如果我更改谷歌浏览器“f12”中的分辨率,它仍然很糟糕。

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>
<body>

  <div id="box-01" class="box"><p> header </p></div>
  <div id="box-02" class="box"><p> main </p>
    <div id="box-05" class="box">   
     <figure>
        <iframe width="560" height="315" src="https://www.youtube.com/embed/nscrstWZz3M" frameborder="0" allowfullscreen></iframe>
        </figure>



</div>
    <div id="box-06" class="box"><p> video1 </p></div>
    <div id="box-07" class="box">


    </div>
    <div id="box-08" class="box"><p> video2 </p></div>
  </div>

  <div id="box-09" class="box"><p> footer </p></div>


</body>
</html>

/* css reset */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}




/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
    display: block;
}







* {
    box-sizing: border-box;
}





body {
  background-color: #333;
  font-size: 1rem;
}



#box-01 {
  background-color: hsla(60, 100%, 60%, .85);

}

#box-02 {
  background-color: hsla(84, 84%, 56%, .85);
}



#box-04 {
  background-color: hsla(279, 60%, 44%, .85);
}



figure {
    height: 0;
    padding-bottom: 56.25%;
    position: relative;
}

  figure iframe {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
}
@media (max-width: 768px) { 


  figure {
    height: 0;
    padding-bottom: 56.25%; /* 16:9 aspect ratio */
    position: relative;
}

  figure iframe {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
}

}

.box {
   position:absolute;
}

p {

    font-family:"news_gothic";
    font-size:1.3vw;
    font-size-adjust:auto;

    width:90%;

    color:white;
}




#box-01 {
  background-color: hsla(60, 100%, 60%, .85);
  left: 0;
  right: 0;
  height: 9.259%;

}



}
body {
   font-family: Merriweather, 'Times New Roman', serif;
  font-size: 1.125rem;
  line-height: 2rem;
  color: #333;

}


#box-02 {
  background-color: hsla(84, 84%, 56%, .85);
  top: 9.300%;
  left: 0;
  width: 70%;
  height: 74.15vh;
}
@media (max-width: 768px) {

    #box-02 {
    float: right;
  background-color: hsla(84, 84%, 56%, .85);
  top: 9.300%;
  left: 0;
  width: 100%;
  height: 56.15vh;
    max-width: 768px;

}

}


#box-05 {
  background-color: hsla(84, 84%, 56%, .85);
  top: 24.300%;
  left: 4.7%;
  width: 32%;
  height: 41.60vh;
}
@media (max-width: 768px) { 

  #box-05 {
  background-color: hsla(84, 84%, 56%, .85);
  top: 24.300%;
  left: 0;
  width: 47%;
  height: 41.12vh;
    max-width: 768px;
     }


}
#box-06 {
  background-color: hsla(50, 50%, 56%, .85);
  top: 11.150%;
  left: 4.7%;
  width: 32%;
  height: 6.11vh;
}
@media (max-width: 768px) { 

  #box-06 {
 background-color: hsla(50, 50%, 56%, .85);
  top: 11.150%;
      left: 0;
  width: 47%;
  height: 6.11vh;
    max-width: 768px;

  }

}
#box-07 {
  background-color: hsla(84, 84%, 56%, .85);
  top: 24.300%;
  left: 49%;
  width: 32%;
  height: 41.60vh;
}
@media (max-width: 768px) { 

  #box-07 {
  background-color: hsla(84, 84%, 56%, .85);
  top: 24.300%;
  left: 52%;
  width: 48%;
  height: 41.12vh;
    max-width: 768px;
    position:absolute;


  }

}
#box-08 {
  background-color: hsla(50, 50%, 56%, .85);
  top: 11.150%;
  left: 49%;
  width: 32%;
  height: 6.11vh;
}
@media (max-width: 768px) { 

  #box-08 {
 background-color: hsla(50, 50%, 56%, .85);
  top: 11.150%;
  left: 52%;
  width: 48%;
  height: 6.11vh;
    max-width: 768px;

  }

}

#box-09 {
  background-color: hsla(50, 50%, 56%, .85);
  right: 0;
  left: 0;
  bottom: 0;
  height: 16.58vh;
}

}

为什么不垂直填充是什么问题?

最佳答案

您在 #box-05 上设置了固定高度,因此固定高度没有响应,因为它不会随着视频的宽高比缩放。删除它并让它符合其中包含响应式 youtube 嵌入的 图形

/* css reset */
html, body, div, span, applet, object, iframe,
h1, h2, h3, h4, h5, h6, p, blockquote, pre,
a, abbr, acronym, address, big, cite, code,
del, dfn, em, img, ins, kbd, q, s, samp,
small, strike, strong, sub, sup, tt, var,
b, u, i, center,
dl, dt, dd, ol, ul, li,
fieldset, form, label, legend,
table, caption, tbody, tfoot, thead, tr, th, td,
article, aside, canvas, details, embed,
figure, figcaption, footer, header, hgroup,
menu, nav, output, ruby, section, summary,
time, mark, audio, video {
    margin: 0;
    padding: 0;
    border: 0;
    font-size: 100%;
    font: inherit;
    vertical-align: baseline;
}




/* HTML5 display-role reset for older browsers */
article, aside, details, figcaption, figure,
footer, header, hgroup, menu, nav, section {
    display: block;
}







* {
    box-sizing: border-box;
}





body {
  background-color: #333;
  font-size: 1rem;
}



#box-01 {
  background-color: hsla(60, 100%, 60%, .85);

}

#box-02 {
  background-color: hsla(84, 84%, 56%, .85);
}



#box-04 {
  background-color: hsla(279, 60%, 44%, .85);
}



figure {
    height: 0;
    padding-bottom: 56.25%;
    position: relative;
}

  figure iframe {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
}
@media (max-width: 768px) { 


  figure {
    height: 0;
    padding-bottom: 56.25%; /* 16:9 aspect ratio */
    position: relative;
}

  figure iframe {
    position: absolute;
    top: 0;
    left: 0;
    height: 100%;
    width: 100%;
}

}

.box {
   position:absolute;
}

p {

    font-family:"news_gothic";
    font-size:1.3vw;
    font-size-adjust:auto;

    width:90%;

    color:white;
}




#box-01 {
  background-color: hsla(60, 100%, 60%, .85);
  left: 0;
  right: 0;
  height: 9.259%;

}



}
body {
   font-family: Merriweather, 'Times New Roman', serif;
  font-size: 1.125rem;
  line-height: 2rem;
  color: #333;

}


#box-02 {
  background-color: hsla(84, 84%, 56%, .85);
  top: 9.300%;
  left: 0;
  width: 70%;
  height: 74.15vh;
}
@media (max-width: 768px) {

    #box-02 {
    float: right;
  background-color: hsla(84, 84%, 56%, .85);
  top: 9.300%;
  left: 0;
  width: 100%;
  height: 56.15vh;
    max-width: 768px;

}

}


#box-05 {
  background-color: hsla(84, 84%, 56%, .85);
  top: 24.300%;
  left: 4.7%;
  width: 32%;
}
@media (max-width: 768px) { 

  #box-05 {
  background-color: hsla(84, 84%, 56%, .85);
  top: 24.300%;
  left: 0;
  width: 47%;
    max-width: 768px;
     }


}
#box-06 {
  background-color: hsla(50, 50%, 56%, .85);
  top: 11.150%;
  left: 4.7%;
  width: 32%;
  height: 6.11vh;
}
@media (max-width: 768px) { 

  #box-06 {
 background-color: hsla(50, 50%, 56%, .85);
  top: 11.150%;
      left: 0;
  width: 47%;
  height: 6.11vh;
    max-width: 768px;

  }

}
#box-07 {
  background-color: hsla(84, 84%, 56%, .85);
  top: 24.300%;
  left: 49%;
  width: 32%;
  height: 41.60vh;
}
@media (max-width: 768px) { 

  #box-07 {
  background-color: hsla(84, 84%, 56%, .85);
  top: 24.300%;
  left: 52%;
  width: 48%;
  height: 41.12vh;
    max-width: 768px;
    position:absolute;


  }

}
#box-08 {
  background-color: hsla(50, 50%, 56%, .85);
  top: 11.150%;
  left: 49%;
  width: 32%;
  height: 6.11vh;
}
@media (max-width: 768px) { 

  #box-08 {
 background-color: hsla(50, 50%, 56%, .85);
  top: 11.150%;
  left: 52%;
  width: 48%;
  height: 6.11vh;
    max-width: 768px;

  }

}

#box-09 {
  background-color: hsla(50, 50%, 56%, .85);
  right: 0;
  left: 0;
  bottom: 0;
  height: 16.58vh;
}

}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width">
  <title>JS Bin</title>
</head>

<body>

  <div id="box-01" class="box">
    <p> header </p>
  </div>
  <div id="box-02" class="box">
    <p> main </p>
    <div id="box-05" class="box">
      <figure>
        <iframe width="560" height="315" src="https://www.youtube.com/embed/nscrstWZz3M" frameborder="0" allowfullscreen></iframe>
      </figure>


    </div>
    <div id="box-06" class="box">
      <p> video1 </p>
    </div>
    <div id="box-07" class="box">


    </div>
    <div id="box-08" class="box">
      <p> video2 </p>
    </div>
  </div>

  <div id="box-09" class="box">
    <p> footer </p>
  </div>


</body>

</html>

关于html - 我的响应式 youtube 嵌入视频没有填满整个框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44555388/

相关文章:

javascript - 无法更改文本框颜色?

javascript - 如何在 react js中使用json在表中显示数据?

html - 边距 : box offset from other boxes in row

javascript - 使Vimeo或youtube在模式打开时播放,并在模式关闭时停止视频播放

javascript - 播放 youtube 视频时隐藏 div

javascript - 如何使用jquery计算html表中的行

html - 不能将 fontface 用于自定义字体

jquery - 修改应用于悬停状态的 CSS

javascript - 使用 slick 从 li 元素创建轮播

YouTube 订阅源 V3