2010-09-14 29 views
0

我有一個像CSS定位

<div id="top" style="height: 50px"></div> 
<div id="bottom" style="height: 50px"></div> 
<div id="content">Here goes some text</div> 

我想中間的div(#bottom網頁)一些HTML代碼將被定位在DIV(#top返回)的頂部,而這樣做,#內容DIV應該自動向上移動50px。

如果像

<div id="bottom" style="height: 50px; position: relative; top: -50px;"></div> 

的#bottom網頁的div我代碼做向上移動,但#內容股利留下的..離開之間的差距50像素。

那麼我該如何定位?

回答

2

如果我的理解正確,你想採取#bottom並將其從常規頁面流中刪除,將其置於#top之上。

兩種方法採取一個元素了正常的網頁流量是position:float;position:absolute;

不知道你的頁面的其餘部分看起來像我建議是這樣的:

<div id='container' style='position:relative'> 
    <div id="top" style="height: 50px"></div> 
    <div id="bottom" style="height: 50px; position:absolute; top:0em; left:0em;"></div> 
    <div id="content">Here goes some text</div> 
</div> 

,將放在#bottom在頂部,#container的左上角,這也是#top所在的位置。作爲常規頁面流的一部分的#container將在#top的正下方。

對於中心絕對定位的元素,你可以這樣做:

<div style="display:table; margin: 0 auto;"> <!-- display:table; to 'shrink-wrap' the div - margin: 0 auto; to center it-> 
    <div style="position: relative; float:left;"> <!-- float also shrink-wraps --> 
     <div id='top'>top div content</div> 
     <div id='bottom' style="position:absolute; top:0em; width:100%; text-align:center;"> content of bottom div </div> 
     <div id='content'></div> 
    </div> 
</div> 
+0

謝謝,是的..這正是我的意思。但是現在我想要中間的#bottom。我正在使用m「argin:0 auto;」,現在它不工作。所以我怎麼把它放在頁面中間..或者#container? – ptamzz 2010-09-14 14:57:55

+1

你想'以一個未知大小的絕對定位元素爲中心'。查看我的更新回答 – SooDesuNe 2010-09-14 23:39:33

+0

謝謝。這幫助了我! – ptamzz 2010-09-16 08:12:20