2011-04-06 70 views
0

我希望在頁面上有一個登錄表單。一個例子是here在頁面上創建一個DIV作爲中心焦點

我知道如何中心我不能工作了一個元素是如何在頁面的中心始終中心的元素,即使在瀏覽器窗口改變大小

+0

你知道汽車財產的利潤率嗎?這爲您提供了靈活的利潤率,您也可以在其他情況下使用。 – 2011-04-06 19:50:42

+0

給出的示例(onehub)最棒的地方在於它使用的方法沒有在任何解決方案中描述過(但...)。他們使用CSS來設置div的顯示屬性來模擬表格的屬性。這種方法雖然有爭議。 – Guttsy 2011-04-06 19:57:07

回答

2

經典問題。下面是一些示例CSS:

#your_element{ 
    position: fixed; 
    left: 50%; 
    top: 50%; 
    width: 600px; 
    height: 400px; 
    margin-left: -300px; 
    margin-top: -200px; 
} 

重要位:負邊距應該是相應尺寸的一半。

+0

謝謝,就是這樣 – Jon 2011-04-06 19:48:46

0

我把這個模板HTML只是針對這種情況,當我需要一個容器,是垂直和水平居中:

CSS:

html, body { 
     height: 100%; 
    } 

    body { 
     background: #ffc; 
     margin: 0; 
     padding: 0; 
    } 

    #vertical-center { 
     float: left; 
     height: 50%; 
     width: 100%; 
     margin-top: -185px; 
    } 

    #content { 
     background: #ffffde; 
     border: 2px dashed red; 
     clear: both; 
     margin: 0 auto; 
     padding: 10px; 
     height: 350px; 
     width: 500px; 
    } 

HTML:

<div id="vertical-center"></div> 
<div id="content"> 
    <h1>Centered Content</h1> 
    <p>This content is centered on the page.</p> 
    <p>More importantly, it won't get cut off when the browser window becomes too small to display it.</p> 
</div> 

請注意,#垂直中心的margin-top必須是#content div的一半高度,並且必須是負值。