【问题标题】:Bootstrap Jumbotron Background Image Not ResponsiveBootstrap Jumbotron 背景图像不响应
【发布时间】:2016-12-13 00:28:20
【问题描述】:

我对 bootstrap 还很陌生,很难在不显示白色背景的情况下让我的 jumbotron 背景图像正确缩放。我在页面顶部使用 Bootstrap Fixed Nav,在中间使用 Jumbotron (bg image) 和 Sticky Footer。当我在谷歌浏览器中测试布局是否正确时,图像似乎没有延伸到页脚的顶部,它显示了白色背景。如何让背景图片在所有设备上正确缩放?

HTML:

    <!-- Fixed navbar -->
    <nav class="navbar navbar-default navbar-fixed-top">
      <div class="container">
        <div class="navbar-header">
          <button type="button" class="navbar-toggle collapsed" data-toggle="collapse" data-target="#navbar" aria-expanded="false" aria-controls="navbar">
            <span class="sr-only">Toggle navigation</span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
            <span class="icon-bar"></span>
          </button>
          <a class="navbar-brand" href="index.html">Jason<span>Murray</span></a>
        </div>
        <div id="navbar" class="navbar-collapse collapse">
          <ul class="nav navbar-nav navbar-right">
            <li class="active"><a href="index.html">Home</a></li>
            <li><a href="#about">About</a></li>
            <li><a href="#contact">Portfolio</a></li>
            <li><a href="#contact">Contact</a></li>
          </ul>
        </div><!--/.nav-collapse -->
      </div>
    </nav>

    <div class="jumbotron">
      <div class="container">
        <h1 class="text-center">Front-End Web Developer</h1>
        <p class="text-center">I'm Jason Murray, Front-End Web Developer who loves to write clean codes and build simplistic web designs.</p>
        <p class="text-center"><a class="btn btn-primary btn-lg" href="#" role="button">Recent Work</a></p>
      </div>
    </div>

    <footer class="footer">
      <div class="container">
        <p class="text-center">Copyright &copy; 2016 <a href="index.html" target="_blank">Jason Murray Web Design</a>. All Rights Reserved</p>
      </div>
    </footer>

    <!-- jQuery (necessary for Bootstrap's JavaScript plugins) -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
    <!-- Include all compiled plugins (below), or include individual files as needed -->
    <script src="js/bootstrap.min.js"></script>
  </body>
</html>

CSS:

.jumbotron {
  background: url("../img/lucho-49.jpg") no-repeat center center;
  -webkit-background-size: 100% 100%;
  -moz-background-size: 100% 100%;
  -o-background-size: 100% 100%;
  background-size: 100% 100%;
}

【问题讨论】:

    标签: html css twitter-bootstrap-3 responsive-design


    【解决方案1】:

    由于 jumbotron 的预定义属性 ma​​rgin-bottom: 30px;,图像未向下拉伸到页脚顶部。

    在您的 CSS 代码中进行以下更改:

    nav + .jumbotron {
      margin-bottom: 0;
      background: url("../img/lucho-49.jpg") no-repeat center center;
      -webkit-background-size: 100% 100%;
      -moz-background-size: 100% 100%;
      -o-background-size: 100% 100%;
      background-size: 100% 100%;
    }
    

    如果您在 .jumbotron 选择器中添加 ma​​rgin-bottom: 0; ,由于其特殊性,它将不起作用。因此,我通过添加 Adjacent Sibling Selector, nav + .jumbotron 来增加特异性,以使用预定义的引导程序的 .jumbroton CSS 属性覆盖您的 CSS 属性。

    【讨论】:

      【解决方案2】:

      这个jumbotron 类的底部有一个边距,所以删除margin

      .jumbotron {
        margin-bottom: 0;
      }
      

      这里是小提琴:fiddle link

      【讨论】:

      • 嗨特乌塔!感谢您的回复!不幸的是,这也不起作用。好像我的背景图片没有显示全高。
      • @JasonMurray,根据您的需要,您说要删除页脚顶部的空白,对吗?你还想做什么?
      • @JasonMurray,再次查看我更新的小提琴链接!
      • 背景图像的问题是它没有显示图像的完整高度。例如,如果您查看 Bootstrap 入门模板,则 jumbotron 会停在中间。我的背景图片也在做同样的事情。我希望 jumbotron 背景图像在 Fixed Nav 和 Sticky Footer 之间显示完整的宽度和高度。
      • @JasonMurray,转到我更新的小提琴链接!这是您要找的吗?
      猜你喜欢
      • 2014-03-26
      • 1970-01-01
      • 2015-02-15
      • 2017-07-20
      • 1970-01-01
      • 2014-09-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多