2017-10-13 132 views
0

我有一個HTML文件,然後我設置背景圖片網址,但圖像沒有填滿瀏覽器的寬度:背景圖像寬度不工作

enter image description here

我也嘗試設置width屬性使其更寬,但它似乎沒有效果。

<!DOCTYPE html> 
<html lang="en"> 
<head> 
    <meta charset="UTF-8"> 
    <title>Title</title> 
    <style> 
     #bg { 

      height:1500px; 
      background: url("img/timg7.jpg") center top no-repeat; 
      width:1800px; 
     } 
    </style> 

</head> 
<body id="bg"> 

<div style="width:400px; height: 200px; background-color: antiquewhite"> 
    BLOCK 
</div> 

</body> 
</html> 
+1

你給了你的身體寬度?那是什麼? – PHPglue

回答

4

需要在此的background-size屬性。

你的CSS應該是:

#bg { 

    background: url("img/timg7.jpg"); 
    background-size: cover; 
    background-repeat: no-repeat; 

} 
0

在這種情況下,你應該使用背景大小,有一個背景大小demo

嘗試使用:

background-size: 120%; 
0

嘗試使用: 背景尺寸:蓋;

並請刪除這些高度和寬度

0

試試這個:

#bg { 

    background: url("img/timg7.jpg") center top no-repeat; 
    background-size: cover; 
} 
0
#bg{ 
    background: url("img/timg7.jpg"); 
    background-repeat:no-repeat; 
    background-size:contain; 
    background-position:center; 
} 
0

你並不需要在你的body的ID,如document.body與JavaScript的得到它,你可以在CSS中使用body。當然,這不是你的問題。請參見下面的代碼:

html,body{ 
padding:0; margin:0; 
} 
body{ 
    background:url(img/timg7.jpg) no-repeat; background-size:cover; 
} 

順便說一句,你應該使用外部CSS,所以它的緩存到瀏覽器內存。