【问题标题】:Make background image responsive使背景图像响应
【发布时间】:2014-04-28 16:08:03
【问题描述】:

我想让背景图像在宽度和高度上都具有响应性,以便我可以在移动设备上运行它。

我用过这个

.your_background_class_name {
    width: 100%;
    height:auto;
    top: 0px;
    left: 0px;
    z-index: -1;
    position: absolute;

}

但它只适用于宽度而不适用于高度。我应该做些什么改变才能让它发挥作用?

【问题讨论】:

标签: css responsive-design background-image


【解决方案1】:

这对我有用:

#header {
background-image: url('http://www.fillmurray.com/g/1920/1080');
background-repeat:no-repeat;
background-size: 100%;
background-position:top left;
position: relative;
}

#header .container {
height: 893px;
position: relative;
}

#header .div1 {
position: absolute;
top: 20px;
}

#header .div2 {
position: absolute;
bottom: 20px;
right: 20px;
}
<div id="header">
<div class="container">
    <div class="div1">
        Top left content
    </div>
    <div class="div2">
        Bottom right content
    </div>
</div>

【讨论】:

    【解决方案2】:

    下面的代码应该可以解决问题:

    class_name {
        background: url(image);
        background-size: 100% auto contain;
    }
    

    【讨论】:

    • 这似乎是不允许的,因为 background-size 需要一个单一的值:包含、覆盖、继承或初始
    【解决方案3】:
    html { 
      background: url(https://encrypted-tbn2.gstatic.com/images?q=tbn:ANd9GcSvctt0I--skoKQVfuVt-i4Et6vQ5ve7lXPkLy9sTElCWLKh1Ps) no-repeat center center fixed; 
      -webkit-background-size: cover;
      -moz-background-size: cover;
      -o-background-size: cover;
      background-size: cover;
    }
    

    了解更多关于background-size here

    See demo

    【讨论】:

    • 非常感谢@newTag。这就是我想要的。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-12-16
    • 1970-01-01
    • 1970-01-01
    • 2022-12-10
    相关资源
    最近更新 更多