简体   繁体   English

CSS3:为背景图像设置高度和宽度

[英]CSS3 : Give height and width to background image

I want to give background size as 636px and 1140px to background image instead of div. 我想给背景图像而不是div设置636px和1140px。 as I don't want the scroll of div because of its height. 因为我不想要div的滚动条,因为它的高度。

If I give height and width to the parent div then I get background-image but when i give to background-size then it doesn't work. 如果我给父div设置高度和宽度,那么我会得到背景图像,但是当我给background-size设置时,它将不起作用。

 .custom_app_phone_div { margin: 5% auto 0 auto; height:auto; width:auto; } .custom_app_phone_div .custom_app_phone_bg_image_div { background: url("http://i.imgur.com/jIE5Bf7.png") no-repeat; background-position-x: center; background-size:636px 1140px;; width: 100%; height: 100%; padding: 10px; } 
 <div class="custom_app_phone_div"> <div class="custom_app_phone_bg_image_div"> </div> </div> 

Any help would be great. 任何帮助都会很棒。

Thank you. 谢谢。

If not defined the div's height will be determined by the content's height. 如果未定义,则div的高度将由内容的高度决定。

What you could do is set the min-heigh property. 您可以设置min-heigh属性。

Set the size of background images to 636px x 1140px using any image editor and then use: 使用任何图像编辑器将背景图像的大小设置为636px x 1140px ,然后使用:

background: url("http://i.imgur.com/jIE5Bf7.png") center no-repeat;

Working fiddle here 在这里工作

I'm not sure what you are asking for. 我不确定您要什么。 But if you want to change the background image size without changing the div size you should either use an image <img/> or use a helper div as background (see example below). 但是,如果要更改背景图像大小而不更改div大小,则应使用图像<img/>或使用辅助div作为背景(请参见下面的示例)。

Now, if you want to get rid of the scrolling bar you can set the parent container .custom_app_phone_div a height/width and set overflow: hidden . 现在,如果要摆脱滚动条,可以将父容器.custom_app_phone_div设置为高度/宽度,并设置overflow: hidden

Can you try this and let us know if this works for you? 您可以尝试一下,让我们知道这是否适合您吗?

 .custom_app_phone_div { margin: 5% auto 0 auto; height:auto; width:auto; } .custom_app_phone_div .bgHelper{ background: url("http://i.imgur.com/jIE5Bf7.png") no-repeat; background-position-x: center; background-size: contain; height: 1140px; width: 636px; } .custom_app_phone_div .custom_app_phone_bg_image_div { } 
 <div class="custom_app_phone_div"> <div class="bgHelper"></div> <div class="custom_app_phone_bg_image_div"> </div> </div> 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM