问题
I want to make background image responsive both in width as well as in height so that i can run it in mobile.
I have used this
.your_background_class_name {
width: 100%;
height:auto;
top: 0px;
left: 0px;
z-index: -1;
position: absolute;
}
but it works in width only not in height. What changes should i do to make it work?
回答1:
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;
}
Know more about background-size
here.
See demo
回答2:
The following code should do the trick:
class_name {
background: url(image);
background-size: 100% auto contain;
}
回答3:
This worked for me:
#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>
来源:https://stackoverflow.com/questions/22556250/make-background-image-responsive