[英]stretch image background on a div or full backgound image in a div
I have this html structure 我有这个HTML结构
<div id="bg">
</div>
and a css 和一个CSS
#bg{
background: transparent url(images/bg.png) no-repeat center center;
background-size: 100%;
width: 300px;
height: 300px;
}
as you can see from the above codes, this 'background-size: 100%;' 从上面的代码中可以看到,此“背景大小:100%;” should make the background image in the div(#bg) fill the whole container of (#bg) but unfortunately, it doesnt work at all even i also tried 'background-size: cover;' 应该使div(#bg)中的背景图像充满(#bg)的整个容器,但是不幸的是,即使我也尝试过'background-size:cover;',它也不起作用。 or 'background-size: contain;' 或“背景大小:包含;” but still no lucks, nothing works. 但仍然没有运气,什么都没有。 Is there any way or alternative way that I could make the background image in the div(#bg) full or will fill the container or will stretch proportionally as what the width and height of the div(#bg)? 有什么方法或其他方法可以使div(#bg)中的背景图像变满或填充容器,或按div(#bg)的宽度和高度成比例地拉伸?
Actually you are forgetting a parameter in your property declaration 实际上,您忘记了属性声明中的参数
background-size: 100% 100%;
Explanation: You are using background-size: 100%;
说明:您正在使用background-size: 100%;
which will suffice only for x
and not for y
so you need to have 2 100%
, 1 for the x
and other for y
这仅适用于x
而不适用y
因此您需要2 100%
, x
1, y
其他
Well, you have this solution background-size:100% 100%; 好吧,您有这个解决方案的背景大小:100%100%;
But it doesn't work in all browsers, if you want the fix for IE8,7 and 6 (I think) you ca try this: 但这并不适用于所有浏览器,如果您想修复IE8,7和6(我认为),可以尝试以下方法:
filter: progid:DXImageTransform.Microsoft.AlphaImageLoader(src='image.jpg', sizingMethod='scale');
-ms-filter: "progid:DXImageTransform.Microsoft.AlphaImageLoader(src='image.jpg', sizingMethod='scale')";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.