使背景图像响应

时间:2014-03-21 10:30:39

标签: css responsive-design background-image

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

我用过这个

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

}

但它的宽度仅适用于高度。我应该做些什么改变才能使它发挥作用?

3 个答案:

答案 0 :(得分:26)

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

答案 1 :(得分:0)

以下代码可以解决这个问题:

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

答案 2 :(得分:-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>