2013-01-09 258 views
7

我有一個大的圖像被用作頁面的background-image。我想要的是圖像的高度將被拉伸以填充頁面的高度。它也應該居中。CSS拉伸背景圖片

background: black url("/image/background.jpg") no-repeat fixed center; 

回答

16

background-size: cover會做的伎倆在現代瀏覽器 - 詳細信息請參閱Mozilla的documentation

對於舊的瀏覽器(尤其是舊版本的IE),我已經有很多成功的使用jQuery插件,像this one模仿行爲。

+3

這固定它:'background-size:1024px 100%;' –

+0

很高興能夠t o指向你所需要的方向:) – Kelvin

+0

我似乎無法使用'background'標籤,但'background-size:cover'似乎正常工作。 – sircapsalot

0

它添加到CSS

{ 
background: black url("/image/background.jpg") no-repeat fixed center; 
height: 100%; 
width:100% 
} 
+1

開爾文的答案是完美的,如果圖像實際上是較大的,那麼你想要把它的空間並將裁剪它,設置寬度和高度將拉伸圖像 –

0

我認爲使用div會是獲得所需效果的最簡單方法。

<div id="background"> 
    <img src="/image/background.jpg" class="stretch" alt="" /> 
</div> 
    <style> 
    #background { 
     background-color:#000000; 
     width: 100%; 
     height: 100%; 
     position: fixed; 
     left: 0px; 
     top: 0px; 
     z-index: -1; 
    } 

    .stretch { 
     width:100%; 
     height:100%; 
    } 
    </style>