[英]Make a Background Image Responsive
In my project I have given a background image for a div while the screen size decreases the image width also has to be decrease.I have done to get this as follows 在我的项目中,我给div设置了背景图像,同时屏幕尺寸减小了,图像宽度也必须减小了。
css for the div is : div的CSS为:
.home_content{
margin: 30px auto 20px;
height: auto;
width:920px;
padding:15px 0px 70px 0px;
background: url(images/bottom_bg.png) no-repeat bottom center #fff;
border:10px solid #bababa;
}
now I have wrote one more css for responsiveness, that is 现在我为响应性再写了一个css,即
@media only screen and (max-width: 760px) {
.home_content{
position: relative;
width: 100%;
}
}
but the problem is the background image given for the div remains the same.please help me to correct this. 但问题是给div的背景图像保持不变。请帮助我纠正此问题。
For a background , you might try to use background-size: 对于background,您可以尝试使用background-size:
@media only screen and (max-width: 760px) {
.home_content{
position: relative;
width: 100%;
background-size:100px 100px; /* or %, or cover/contains/auto */
}
}
See: http://www.w3.org/TR/2002/WD-css3-background-20020802/#background-size 参见: http : //www.w3.org/TR/2002/WD-css3-background-20020802/#background-size
This should work most thing including IE9 这应该可以正常工作,包括IE9
@media only screen and (max-width: 760px) {
.home_content {
position: relative;
width: 100%;
-webkit-background-size: cover; /* For WebKit */
-o-background-size: cover; /* Opera */
-moz-background-size: cover; /* Mozilla */
background-size: cover; /* Generic */
}
}
You don't really need media queries for this. 您实际上并不需要媒体查询。 You can use
background-size
and background-image
. 您可以使用
background-size
和background-image
。 Of course you'll have to modify this to suit your needs. 当然,您必须修改此设置以适合您的需求。 I've put
width: 80%;
我把
width: 80%;
to demonstrate the width being applied. 演示要应用的宽度。 You can change it to 100% or whatever your need is.
您可以将其更改为100%或任何需要。
#container{
width: 500px;
height: 500px;
background-image: url('http://upload.wikimedia.org/wikipedia/commons/8/87/Ducati_999_2005.jpg');
background-repeat: no-repeat;
border: 1px solid #000;
background-size: 80%;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.