简体   繁体   English

没有基于px的宽度的background-image / background-size

[英]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%;
}

JSFIDDLE 的jsfiddle

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.

相关问题 如何使用背景大小来填充整个 div 而不会破坏背景图像? - how to use background-size to fill whole div without breaking with background-image? 具有background-size:cover的可拖动背景图像 - Draggable background-image with background-size:cover Chrome动画gif背景图像不播放 - 背景大小 - Chrome animated gif background-image not playing - background-size 如何像背景大小的背景图像一样全屏拉伸图像:封面? - How stretching an image fullscreen like a background-image with background-size: cover? 背景尺寸的 Svg 背景宽度问题 - Svg background width problem with background-size 将标题中的背景图像比例级别与背景大小匹配:cover - Match background-image scale-level in header over background-size:cover 如何由于背景尺寸(封面)而停止放大背景图像? - How do I stop background-image from zooming in because of background-size: cover? background-size:封面无法完全用背景图像填充div - background-size: cover is not working to completely fill div with background-image background-size: contain 不显示背景图片的情况 - Conditions when background-size: contain doesn't display background-image 使用“background-size : 100% 100%”时,DIV“background-image”没有在整个DIV上拉伸 - DIV "background-image" not stretched over entire DIV when using "background-size : 100% 100%"
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM