2011-01-14 55 views
0

我有以下標記:飄來背景DIV影響空間

<div id="container"> 
    <img src="images/behind.png" id="behind" /> 
    <div id="header"> 
    </div> 
</div> 

具有以下樣式: 的#container { 保證金左:自動; margin-right:auto; width:800px; }

#container #behind{ 
    float: right; 
    margin-top: -50px; 
    margin-right: -20px;  
} 


#header { 
    border: #ACACAC 1px solid; 
    background: silver url('../images/headerbg.png') repeat-x; 
    height: 100px; 
    margin-top: 50px; 
    position: relative; 
    width: auto; 
    margin-right: 0px; 
} 

現在,這似乎是我想它;但是,當我將登錄框添加到標題並將其右移時,它會移動到標題容器的右側,但在#behind中大約200px,並且圖像的寬度被填充到右側。

對此提出建議?

回答

0

有可能是一個圖像浮動旁邊的問題...嘗試將圖像放在一個然後浮動。

例如:

<div id="container"> 
    <div id="behind"><img src="images/behind.png" /></div> 
    <div id="header"></div> 
</div> 
+0

嗨,亞當,歡呼聲,但沒有解決它。 – James 2011-01-14 07:00:48

0

據我瞭解你的情況,你有兩個元素,都浮動到右邊。這將導致這些元素彼此相鄰。看看例4這裏:http://j-cafe.com/floats.html

如果你希望#behind中的圖像在背景中,並在其上的登錄框,你應該##背後的圖像設置爲#container div中的背景圖像,就像你用headerbg.png做的一樣。您可以像這個例子中一樣將其右對齊:

body {background:#ffffff url('img_tree.png') no-repeat right top;} 

這樣它就不會干擾登錄框了。