【问题标题】:Bootstrap responsive embed disappears when inside flexbox?Bootstrap 响应式嵌入在 flexbox 内消失了吗?
【发布时间】:2017-02-14 23:47:38
【问题描述】:

我正在使用 flexbox 将里面的项目居中对齐(因为其他任何东西似乎都不起作用)。尽管引导响应嵌入似乎完全消失了,但我不知道为什么。我不能在嵌入上使用position: relative,因为这会阻止它响应。这是它的代码...

.embed-responsive {
  /*position: initial;*/ /* if set to initial or anything other than relative is re-appears but is no longer responsive */
}

body {
  background: grey;
}

div.title-area {
    height: 350px;
    width: 100%;
    position: relative;
    padding: 0 50px 0 50px;
}

div.title-area .video {
    box-shadow: 0 5px 15px rgba(0, 0, 0, .3);
}

div.title-area > .col-md-6 {
    min-height: 100%;
    display: flex;
    align-items: center;
}

div.title-area h1 {
    text-shadow: 0 3px 6px rgba(0, 0, 0, .35);
    margin: 0;
    text-transform: uppercase;
    /*vertical-align: middle;
    display: table-cell;
    position: relative;*/
}

.title-area h1 > small{
    color: white;
    font-size: 15px;
    text-indent: 5px;
    display: block;
    opacity: .6;
    text-transform: uppercase;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
<div class="title-area">
    <div class="col-md-6">
        <h1>main title<small>subtitle</small></h1>
    </div>
    <div class="col-md-6">
        <div class="embed-responsive embed-responsive-16by9 video">
          <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/rqEy5_W6YHQ?showinfo=0"></iframe>
          </div>
    </div>
</div>

JSFiddle

【问题讨论】:

    标签: css twitter-bootstrap flexbox


    【解决方案1】:

    看看Grid system,您可以在两行中插入“col-md-6”,如下面的片段:

    body {
      background: grey;
    }
    
    div.title-area {
      height: 350px;
      width: 100%;
      position: relative;
      padding: 0 50px 0 50px;
    }
    
    div.title-area .video {
      box-shadow: 0 5px 15px rgba(0, 0, 0, .3);
    }
    
    div.title-area > .col-md-6 {
      min-height: 100%;
      display: flex;
      align-items: center;
    }
    
    div.title-area h1 {
      text-shadow: 0 3px 6px rgba(0, 0, 0, .35);
      margin: 0;
      text-transform: uppercase;
      /*vertical-align: middle;
      display: table-cell;
      position: relative;*/
    }
    
    .title-area h1 > small{
      color: gray;
      font-size: 15px;
      text-indent: 5px;
      display: block;
      opacity: .6;
      text-transform: uppercase;
    }
    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
    <script src="https://code.jquery.com/jquery-2.1.1.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
    
    
    <div class="title-area">
        <div class="row">
            <div class="col-xs-6">
                <h1>main title<small>subtitle</small></h1>
            </div>
            <div class="col-xs-6">
                <div class="embed-responsive embed-responsive-16by9 video">
                    <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/rqEy5_W6YHQ?showinfo=0"></iframe>
                </div>
            </div>
        </div>
    </div>

    【讨论】:

    • 这是正确的,它需要用 .row div 包装 .col div 以使其正常工作
    • 无论分辨率如何,将它们放在行中都会使它们堆叠,我想要做的是让 2 列并排。
    • @Shixma 你只能使用一行和 col-xs 而不是 col-md
    【解决方案2】:

    在嵌入响应的 div 周围添加一个 div,以便前者可以在 flex div 中流动,而后者可以具有嵌入响应项所需的相对位置。为新的 div 设置 1 的 flex-grow 样式以调整其大小。

    HTML:

    <div class="title-area">
      <div class="col-md-6">
        <h1>main title<small>subtitle</small></h1>
      </div>
      <div class="col-md-6">
        <div style="flex-grow: 1;">
          <div class="embed-responsive embed-responsive-16by9 video">
            <iframe class="embed-responsive-item" src="https://www.youtube.com/embed/rqEy5_W6YHQ?showinfo=0"></iframe>
          </div>
        </div>
    
      </div>
    </div>
    

    样式:

    .embed-responsive {
      // position: initial;
      /* if set to initial or anything other than relative is re-appears but is no longer responsive */
    }
    
    body {
      background: grey;
    }
    
    div.title-area {
      height: 350px;
      width: 100%;
      position: relative;
      padding: 0 50px 0 50px;
    }
    
    div.title-area .video {
      box-shadow: 0 5px 15px rgba(0, 0, 0, .3);
    }
    
    div.title-area > .col-md-6 {
      min-height: 100%;
      display: flex;
      align-items: center;
    }
    
    div.title-area h1 {
      text-shadow: 0 3px 6px rgba(0, 0, 0, .35);
      margin: 0;
      text-transform: uppercase;
      /*vertical-align: middle;
        display: table-cell;
        position: relative;*/
    }
    
    .title-area h1 > small {
      color: white;
      font-size: 15px;
      text-indent: 5px;
      display: block;
      opacity: .6;
      text-transform: uppercase;
    }
    

    https://jsfiddle.net/rz5cmpz3/2/

    【讨论】:

    • 虽然这确实有效,但不幸的是,这会阻止它保持其纵横比。
    • @Shixma 我已经对其进行了编辑以保留纵横比。
    猜你喜欢
    • 2016-01-23
    • 1970-01-01
    • 2017-09-20
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    相关资源
    最近更新 更多