2015-07-13 212 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