【问题标题】:How to preserve grid layout across devices using Bootstrap?如何使用 Bootstrap 跨设备保留网格布局?
【发布时间】:2018-09-13 06:20:05
【问题描述】:

我正在使用 Bootstrap v.3 构建一个网站,该网站需要跨设备保持一致。我希望在所有设备上保持相同的布局。我使用以下代码并排显示两个 youtube 视频。

<div class="row">
        <div class="col-md-7 col-lg-7 col-sm-7 col-xs-7" id="introduction">
            <p>The project uses Machine Learning to identify the waste items. A brief introduction about the Machine Learning technology is provided in the video below.
            </p>
            <iframe width="500" height="205" src="https://www.youtube.com/embed/yofjFQddwHE" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe> 
        </div>
        <div class="col-md-5 col-lg-5 col-sm-5 col-xs-5">

            <iframe src="https://www.youtube.com/embed/7YgnJELpMyE?ecver=2" width="450" height="305" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
        </div>
    </div>

该网站在桌面上看起来像这样 -

但是,在手机上,它看起来像这样。

如您所见,尽管 col-md-* col-sm-*col-xs-* 的值相同,但布局会发生变化。请帮忙。

【问题讨论】:

  • 你使用的是什么版本的 Bootstrap?
  • ver 3.3 。通过 CDN 导入

标签: html css twitter-bootstrap twitter-bootstrap-3 bootstrap-grid


【解决方案1】:

TwBs v4 用户注意事项:将 col-xs-* 替换为 col-*


原始答案(TwBs v3):您的col-* 课程非常适用。

Bootstrap 是移动优先,这意味着col-xs-7 不需要col-sm-7col-md-7 或更高版本。

您可以通过选择元素并查看它们的bounding-rect-box(大多数开发工具突出显示)来检查它。但是,这并不能阻止 &lt;iframe&gt; 元素(具有硬编码的 widthheight 属性)溢出其父元素的宽度。如果你想覆盖它,你需要将他们的width 设置为100%

iframe {
  display: block;
  width: 100%;
}

看看它的工作原理:

iframe {
  display: block;
  width: 100%;
}
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet" />
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="container">
  <div class="row">
    <div class="col-xs-7" id="introduction">
      <p>The project uses Machine Learning to identify the waste items. A brief introduction about the Machine Learning technology is provided in the video below.
      </p>
      <iframe width="500" height="205" src="https://www.youtube.com/embed/yofjFQddwHE" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
    </div>
    <div class="col-xs-5">
      <iframe src="https://www.youtube.com/embed/7YgnJELpMyE?ecver=2" width="450" height="305" frameborder="0" allow="autoplay; encrypted-media" allowfullscreen></iframe>
    </div>
  </div>
</div>

如果你应用它但它不起作用,这意味着你的项目中更强的选择器对这些属性应用了不同的 CSS 规则,给你两个选择:

  • 查找并禁用这些规则(您可以检查元素以查找当前适用于它的所有规则)
  • 在您的 CSS 中使用更强大(更具体)的选择器(您可以找到大量解释 CSS 特异性原则的在线资源 - 请记住,您的选择器越强大,您以后修改它的难度就越大。理想情况下一个应始终使用应用所需规则的最不具体的选择器,从而确保它们易于应用任何未来的模组)。

注意:很可能,iframe 选择器对于您的项目来说不够强大。您可以使用#id.class,或者,如果您不想通过添加任何额外的类或 id 来更改标记,则可以使用否定来夸大其特异性:

iframe:not(#_):not(#_)` { /* css rules here */ }

【讨论】:

  • 这是一个常见的问题。已回答many times before。您可能应该选择第二个答案,即使用% 值作为padding-bottom 的答案。 Stack Overflow的最大价值在于,常见问题的答案一次给出,遇到类似问题的人都能从中受益。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-10-02
  • 2018-02-20
  • 1970-01-01
  • 2019-07-12
  • 2014-11-07
  • 2012-10-01
相关资源
最近更新 更多