【问题标题】:Responsive background images bootstrap 3响应式背景图像引导程序 3
【发布时间】:2019-05-29 13:22:58
【问题描述】:

我正在使用引导程序并试图使我的背景图像响应,但它只是不起作用!这是我的代码...

html

<div class="row">
    <div class="bg">
        <img src="../img/home_bg.jpg" alt="home background image">
    </div><!-- /.bg -->
</div><!-- /.row -->

css

.bg {
    background-repeat: no-repeat;
    background-position: center center;
    background-attachment: fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

请帮帮我!谢谢!

【问题讨论】:

  • 你能创建一个jsfiddle吗
  • 另外,一个相当明显的错误是 不是背景,因此除非您在 css 中声明背景图像,否则 .bg 中的 css 不会产生影响
  • 谢谢...我之前没有创建过 jsfiddle,我现在看看,我对编码很陌生!
  • 没问题,我们曾经都是初学者!
  • 不幸的是,如果不设置设置高度/宽度等,背景图像无法填充 div。但是鉴于我的时间有限jsfiddle.net/8L9Ty/3,这是我能想到的最好的方法

标签: html css twitter-bootstrap background-image


【解决方案1】:

您需要详细了解背景图像和内嵌图像之间的区别。您不能将 img 标记设置为背景,它是块级元素,并与文档一起流动。背景图像是元素的属性。由于您希望此图像成为整个页面的背景,因此您可以将背景图像应用于 html 或 body 元素。

http://jsfiddle.net/8L9Ty/3/

body {
    background: url('http://placekitten.com/900/900');
    background-repeat: no-repeat;
    background-position: center center;
    background-attachment: fixed;
    -webkit-background-size: cover;
    -moz-background-size: cover;
    -o-background-size: cover;
    background-size: cover;
}

【讨论】:

    【解决方案2】:

    我尝试了这么久,最后我发现在 bootstrap 3.3.5 中你必须使用 height 和 width 属性来设置背景。 我使用了以下代码:

    .header{
        background-image: url(images/header.png); 
        height: 500px;
    
        width: 100%;
        background-repeat: no-repeat;
    
        background-position: center center;
        background-attachment: fixed;
    
        -webkit-background-size: cover;
        -moz-background-size: cover;
    
        -o-background-size: cover;
        background-size: cover;
    }
    

    【讨论】:

      【解决方案3】:

      因为您没有为“DIV”标签提及背景图片。我假设您想为整个页面应用背景图片。这是你要找的吗?

      样式表

       body {
          background: url('http://i.stack.imgur.com/jGlzr.png') repeat-x, repeat-y;
       }
      

      HTML 部分

      <body>
      <div class="row">
      <div class="bg">
      
      </div><!-- /.bg -->
      </div><!-- /.row -->
      </body>
      

      http://jsfiddle.net/AziziMusa/8L9Ty/5/

      【讨论】:

      • 谢谢您.. 是的,我希望图像适合作为整个背景... 我尝试了您所说的,它使图像正确适合,但没有响应!如果有另一种不使用 div 的更简单的方法(这只是我想到的第一件事)那就太好了..
      【解决方案4】:

      删除img标签&lt;img src="../img/home_bg.jpg" alt="home background image"&gt;并使用css如下:

      html

      <div class="row">
        <div class="bg"></div><!-- /.bg -->
      </div><!-- /.row -->
      

      css

      .bg {
          background-image: url('../img/home_bg.jpg');
          background-repeat: no-repeat;
          background-position: center center;
          background-attachment: fixed;
          -webkit-background-size: cover;
          -moz-background-size: cover;
          -o-background-size: cover;
          background-size: cover;
      }
      

      【讨论】:

        【解决方案5】:

                    body, html {
                        height: 100%;
                        margin: 0;
                    }
        
                    .bg {
                        /* Full height */
                        height: 100%;
        
                        /* The image used */
                        background-image: url("https://picsum.photos/id/1/200/300");
        
                        /* Center and scale the image nicely */
                        background-position: center;
                        background-repeat: no-repeat;
                        background-size: cover;
                    }
            <div class="container-fluid bg" >
        
            </div>

        【讨论】:

          猜你喜欢
          • 2013-11-14
          • 1970-01-01
          • 2013-09-19
          • 2013-09-21
          • 1970-01-01
          • 1970-01-01
          • 2013-09-07
          • 1970-01-01
          相关资源
          最近更新 更多