【问题标题】:Float on embed youtube video make it disappear浮动嵌入 youtube 视频使其消失
【发布时间】:2017-09-04 13:30:53
【问题描述】:

我想将 2 个 div 内联。第一个 div 是从 youtube 嵌入的视频。第二个 div 带有一些文本。我尝试使用浮动和清除,但是当我使用它时,视频消失了。我尝试使用 display: inline-block 但我收到了相同的结果。我用clear:both 在底部尝试了一个空的div,结果并不满意。我真的很想知道问题出在哪里?视频容器是响应式的。也许这会导致问题?下面我附上一段代码。

.desc {
  border-left: 4px solid #80d4ff;
  padding: 6px 10px 8px 6px;
  text-indent: 5px;
  background-color: #fff;
}

.video-container {
  position: relative;
  padding-bottom: 56.25%;
  overflow: hidden;
  margin: 15px 0;
}

.video-container iframe,
.video-container object,
.video-container embed {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
}

.videoHndl {
  max-width: 500px;
  margin: 0;
  padding: 0;
  overflow: hidden;
}
<div class="videoHndl">
  <div class="video-container">
    <iframe width="500px" height="280" src="https://www.youtube.com/embed/HRbD4UcK-0I">
            </iframe>
  </div>
  <div class="desc">
    <p><b>Description:</b></p>
    <p>Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text </p>
  </div>
</div>

【问题讨论】:

    标签: html css iframe


    【解决方案1】:

    我会这样做:

    .desc{
        margin-left: 500px;
        border: 1px black solid;
        border-left: 4px solid #80d4ff;
        padding: 6px 10px 8px 6px;
        text-indent: 5px;
        background-color: #fff;
    }
    .video-container {
        float: left;
        clear: left;
        padding-bottom: 56.25%;
        overflow: hidden;
        width: 500px;
        height: 280px;
        border: 1px green solid;
    }
    
     .videoHndl{
        width: 800px;
        height: 400px; /* to be updated to the desired height */
        margin: 0;
        padding: 0;
        overflow: hidden;
       border: 1px blue solid;
    }
    <div class="videoHndl">
        <div class="video-container">
            <iframe width="500px" height="280"
            src="https://www.youtube.com/embed/HRbD4UcK-0I">
            </iframe>
        </div>
        <div class="desc">
            <p><b>Description:</b></p>
            <p>Some text Some text Some text Some text Some text Some text 
            Some text Some text Some text Some text Some text Some text Some 
            text Some text Some text Some text </p>
        </div>
    </div>

    【讨论】:

    • 感谢您的回答!这种方式有效,但是当我调整浏览器大小时,响应不起作用。我应该为此使用@media 查询吗?
    【解决方案2】:

    试试这个css代码

    .desc{
      border-left: 4px solid #80d4ff;
      padding: 6px 10px 8px 6px;
      text-indent: 5px;
      background-color: #fff;
    }
    .video-container {
      overflow: hidden;
      margin: 15px 0;
      width: 100%;
    }
     .videoHndl{
       margin: 0;
       padding: 0;
       overflow: hidden;
       display: flex;
       justify-content: flex-start;
     }
    

    【讨论】:

    • 你的方式可行,但是当我调整浏览器的大小(使其像手机屏幕一样小)时,这个 div 重叠并且响应不起作用。
    • 尝试使用@media 查询使其具有响应性
    【解决方案3】:

    您必须以像素为单位添加宽度,请参见下面的代码,此代码将帮助您修改 css。

    .desc {
      border-left: 4px solid #80d4ff;
      padding: 6px 10px 8px 6px;
      text-indent: 5px;
      background-color: #fff;
      float: left;
      width: 150px;
    }
    
    .video-container {
      position: relative;
      padding-bottom: 56.25%;
      overflow: hidden;
      margin: 15px 0;
      float: left;
      width: 300px;
    }
    
    .video-container iframe,
    .video-container object,
    .video-container embed {
      position: absolute;
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
    }
    
    .videoHndl {
      max-width: 500px;
      margin: 0;
      padding: 0;
      overflow: hidden;
    }
    <div class="videoHndl">
      <div class="video-container">
        <iframe width="300px" height="280" src="https://www.youtube.com/embed/HRbD4UcK-0I">
          </iframe>
      </div>
      <div class="desc">
        <p><b>Description:</b></p>
        <p>Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text Some text </p>
      </div>
    </div>

    【讨论】:

    • Ankit,对不起,我能问你为什么使用这个值作为宽度吗?我尝试更改它们,但现在无法正常工作...
    • 如果没有宽度,div 将自动为全宽,所以我们必须应用 CSS 宽度才能使浮动生效
    【解决方案4】:

    这是因为将 .video-container iframe 的位置设置为绝对位置, .video-container 对象, .video-container 嵌入类。删除参数并设置float:left作为视频句柄,您将能够并排看到两个div。所以你编辑的css应该是这样的。

    .video-container iframe,
    .video-container object,
    .video-container embed {
      top: 0;
      left: 0;
      width: 100%;
      height: 100%;
     }
    .videoHndl{
       max-width: 500px;
       margin: 0;
       padding: 0;
       overflow: hidden;
       float:left;
       }
    

    【讨论】:

    • 感谢您的快速回复,但这对我不起作用。当我删除绝对位置并将 float:left 插入 .videoHndl 时,我附上了 #wrapper 发生的情况。 imgur.com/a/koUxA
    【解决方案5】:

    &lt;div&gt; 标签在渲染时会自动换行,你可能想试试&lt;span&gt; 标签

    【讨论】:

    • 我将“div”标签更改为“span”标签,但结果是一样的。看看下面附上的图片。 imgur.com/a/qOhis
    猜你喜欢
    • 1970-01-01
    • 2016-11-19
    • 2015-06-01
    • 2014-02-07
    • 2011-02-27
    • 2021-12-04
    • 2012-06-25
    • 1970-01-01
    相关资源
    最近更新 更多