带背景图像的Div(100%宽度100%高度)

时间:2014-09-04 06:59:29

标签: css image background

我想要实现的是一个带有背景图像的div,它反映了div的大小,我不关心谁保持宽高比

一些测试

<div style='position:absolute;width:"+obj.width+"px;height:"+obj.height+"px;margin-left:"+obj.left+"px;margin-top:"+obj.top+"px;background-image:url(img.png);background-size: 100%;background-repeat: no-repeat;border:1px solid red;'>;

enter image description here

<div style='position:absolute;width:"+obj.width+"px;height:"+obj.height+"px;margin-left:"+obj.left+"px;margin-top:"+obj.top+"px;background-image:url(img.png);background-size: cover;background-repeat: no-repeat;border:1px solid red;'>;

enter image description here

<div style='position:absolute;width:"+obj.width+"px;height:"+obj.height+"px;margin-left:"+obj.left+"px;margin-top:"+obj.top+"px;background-image:url(img.png);background-repeat: no-repeat;border:1px solid red;'>;

enter image description here

1 个答案:

答案 0 :(得分:2)

试试这个:

background-size: 100% 100%;

OR

background-size:auto 100%;