2012-07-10 53 views
1

當我調整瀏覽器窗口的大小時,我的div元素移動到左側,但我希望它們留在一個位置,當我這樣做時。如何在調整瀏覽器窗口大小的同時讓div元素保留在一個位置?讓div無法繼續移動瀏覽器屏幕

<div style="background:red; width:30px; height:30px"> 

</div> 

回答

1
<div style="background:red; width:30px; height:30px position: absolute; 
      top:/* pixels from top of screen*/ 30px; 
      left:/* pixels from left side of screen*/100px;"> 

</div> 
+0

這種方式將停止DIV,從頁面 – 2012-07-10 23:28:24

+0

這是很難明白你正在嘗試做的運動S中,但我相信這是你想要 – JoeCortopassi 2012-07-10 23:29:19

+0

什麼感謝JoeCortopassi幫助 – 2012-07-10 23:30:21

0

你有幾個選項來實現,但是你想什麼哪一個你會使用取決於你的代碼的其餘部分。

您可以絕對定位容器,但在這種情況下,它非常重要,包含這些div的父元素也具有定義在其上的非靜態定位。最流行的方法之一是定義父元素的相對位置,然後絕對定位內元素。

這裏是一個例子。

HTML

<div class="wrapper"> 
    <div class="div_1"> 
     <!-- DIV 1 content --> 
    </div> 
    <div class="div_2"> 
     <!-- DIV 2 content --> 
    </div> 
</div> 

CSS

.wrapper { 
    position:relative; 
} 

.div_1 { 
    position:absolute; 
    top:10px; 
    left:20px; 
} 
.div_2 { 
    position:absolute; 
    bottom:10px; 
    left:20px; 
} 
相關問題