[英]background-image / background-size without a px based width
i have always the problem that if taking a background-image i am not able to see this image until i give the containing give a fixed width: 我总是有一个问题,如果我拍一张背景图片,直到我给它包含一个固定的宽度,我才能看到这张图片:
<div class="picture">
</div>
.picture {
background-image: url('img/start_bigpic.jpg');
background-size: 100%;
}
This does not work. 这是行不通的。 Even if i update it likes this the image is not shown.
即使我更新它喜欢这样的图像也不会显示。
.picture {
background-image: url('img/start_bigpic.jpg');
background-size: 100%;
width: 100%;
height: 100%;
}
Only if i add a pixel-based width and height the image is shown properly, but i want only percentage based values in my fluid grid. 仅当我添加基于像素的宽度和高度时,图像才能正确显示,但我只希望流体网格中基于百分比的值。
What is the solution for this? 有什么解决方案? Can i show a background-image without having a pixel based width and height of the containing div?
我可以显示背景图像而没有包含div的基于像素的宽度和高度吗?
Thanks! 谢谢!
EDIT: Here is my jsfiddle of my problem. 编辑:这是我的问题我的jsfiddle。 I can't get the picture gets displayed unless i use a px based height: http://jsfiddle.net/t3YZt/
除非我使用基于px的高度,否则我无法显示图片: http : //jsfiddle.net/t3YZt/
Try adding some height to a container, for instance body: 尝试为容器增加一些高度,例如body:
html, body {
width: 100%;
height: 100%;
padding:0;
margin:0;
}
.picture {
background-image:url(http://s23.postimg.org/kewe6boez/test_background_image.png);
background-size: 100%;
width: 100%;
height: 100%;
}
To be sure your background container has any width, you should care of that by your own (setting its height to 100% (of its parent) or any other fixed height, or any auto height dependeintly on children inside). 为确保背景容器具有任何宽度,您应该自己照顾(将其高度设置为其父容器的100%)或任何其他固定高度,或者任何自动高度取决于里面的孩子)。
Then, you do the following for the background: 然后,对背景进行以下操作:
.picture{
background-size:cover; /* this way */
background-size:content; /* or this one */
}
... to stretch the image accordingly to its container size. ...以根据容器的大小拉伸图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.