简体   繁体   English

css:使背景图像适合 div 的高度和宽度

[英]css: Making background image fit height and width of div

in bootstrap template i'm adding an image as background like this在引导模板中,我正在添加一个图像作为背景,就像这样

background:url(../images/banner.png) no-repeat;
background-size: 100%;

problem with these css rules is that images is getting clipped only showing center part of the image which is as per height of the div where its added i want image to be shown to its full height instead of small center part only .这些 css 规则的问题是图像被剪裁仅显示图像的中心部分,这是根据 div 的高度添加的我希望图像显示为其全高度,而不是仅显示小的中心部分。

see screenshot for better understanding查看屏幕截图以更好地理解

在此处输入图片说明

试试这个

background-size: contain;

Try your code to this.试试你的代码。

background:url(../images/banner.png);
background-repeat:  no-repeat;
background-size: 100%;

And add this.并添加这个。

html{
height: 100%;
}

 div { height: 100px; width: 100px; background: url(http://i.ytimg.com/vi/v1SabYdIlZI/maxresdefault.jpg) no-repeat; background-size: 100%; }
 <div></div>

background-size: 100% 100%;

will give you the result you wanted.会给你你想要的结果。

But I don't recommend it.但我不推荐它。

background : url (../images/banner.png) no-repeat; background-size:contain;

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

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