2011-10-11 68 views
0

愚蠢的問題,但我無法找到答案,因爲所有答案都假設在寬度方面居中div。如何在頁面中居中div? (高度寬度)?

我需要的是將div放在高度和寬度的中間位置,這樣在頁面的非常中心位置。

我試過這個,但它只是在頁面寬度沒有高度的方式中心div。

#myDiv { 
    width: 500px; 
    height: 500px; 
    margin: auto; 
} 

那麼我怎樣才能得到它在頁面的非常中心? )))

+0

這是否適合您:http://vidasp.net/CSS-centered-page.html –

回答

2

創建具有ID wrapper#myDiv元件周圍的包裝,並應用此CSS代碼:

#wrapper{ 
    display: table; 
} 
#myDiv{ 
    display: table-cell; 
    vertical-align: middle; /*Vertically centered*/ 
    text-align: center; /* Horizontally centered */ 
} 

此代碼中心任何寬度/高度的元件。

2
position: absolute; 
left: 50%; 
top: 50%; 
margin-left: -250px; /*(half of width)*/ 
margin-top: -250px; /*(half of height)*/ 

應該工作。

+2

先到那裏,這是應該工作。如果寬度/高度是自動的,請查找js選項。還必須注意,父母應該有一個「相對位置」 – rickyduck

相關問題