2012-05-27 82 views
7

可能重複:
Stretch and Scale a CSS image Background - With CSS onlyCSS背景圖像擬合到的div

我想設置的div背景圖像的div(寬度:1000,最小高度:710)和圖像尺寸是753 x 308 whan我試圖設置它圖像不適合div大小我應該怎麼做?

.Invitation { 
    background-color: transparent; 
    background-image: url('../images/form.png'); 
    background-repeat: no-repeat; 
    background-position: left top; 
    background-attachment: scroll; 
    background-size: 100%; 
    width: 1000px; 
    height: 710px; 
    margin-left: 43px; 
} 

的DIV類是

.content { 
    clear:both; 
    width:1000px;  
    background:#e7e8e9; 
    background-image:url('../images/content.jpg'); 
    background-repeat:no-repeat; 
    background-position:left top; 
    min-height:710px; 
} 

而且代碼

<div class="content"><div class="Invitation">form goes here....</div></div> 
+0

您是否想要縮放背景圖像以適應(比例)或拉伸(扭曲)到DIV? – rcdmk

+0

圖像應該覆蓋div。 –

回答

7

嘗試是這樣的:

CSS

<style type="text/css"> 
    img.adjusted { 
    position: absolute; 
    z-index: -1; 
    width: 100%; 
    top: 0; 
    left: 0; 
    } 
    .Invitation { 
    position: relative; 
    width: 1000px; 
    height: 710px; 
    margin-left: 43px; 
    } 
</style> 

HTML

<div class="Invitation"> 
    This is an example 
    <img src="http://files.myopera.com/arshamr/albums/612249/Nature%20(3).jpg" alt="" class="adjusted"> 
</div> 

我希望這有助於!

+1

請注意,負面zindex可能會隱藏某些版本的Firefox中的元素,因爲元素呈現在文檔下方的錯誤。現在可能是安全的,我還沒有用3.6或更新版本進行測試。 – rcdmk

+0

那麼,至少根據標準,Z指數沒有限制,無論是正值還是負值 –

+0

你是對的。 CSS 2.0沒有指定限制,2.1指定允許負值,但舊版本的FF有這個錯誤。 – rcdmk