2015-07-13 86 views
0

我想将div背景图像设置为适合div尺寸。我正在尝试从谷歌帮助很多。但无法获得解决方案。Autofit Div背景图片

我格属性是类似下面

userPic.style.width = "65px"; 
userPic.style.height = "85px"; 
userPic.style.position = "absolute"; 
userPic.style.left = "10px"; 
userPic.style.top = "10px"; 
userPic.style.borderRadius = '4em'; 

现在我设定的底色图像像下面

userPic.style.background = "url('"+employee.profilepic+"') no-repeat"; 
userPic.className = "profilePic"; 

和我设置如下图所示

.profilePic 
{ 
    background-size: 100%; 
    background-repeat: no-repeat; 
    overflow: hidden; 
    margin:auto; 
} 

但背景大小profilepic类:100%在铬合金中工作不正常,而且工作不完美。我有尺寸为65x85的div,我的图像尺寸大于此值。那么如何在这个div中适合大或小的图像呢?

+0

您是否尝试在背景大小中使用'cover'或'contain'属性值? –

+0

我试过了,但是它在调试窗口中显示罢工。这意味着它不起作用。 – VarunJi

回答

2

我会尝试background-size: cover;,看看这是否得到你想要的效果。

+0

background-size:cover;和内容;两者都不起作用。 :( – VarunJi

+0

我现在得到了解决方案,你说得对,但我不应该为此分配任何CSS类名称,那么它就会起作用。 – VarunJi