簡體   English   中英

在頁面中間水平和垂直居中div

[英]Horizontally and vertically center div in the middle of page

我正在嘗試在頁面中間獲得如下所示的頁面布局,其中水平和垂直居中div的頁眉和頁腳固定在頁面的頂部和底部

這在除ie6和ie7之外的所有瀏覽器中都很好用。

誰能幫我解決這個問題? 我是服務器端開發人員,並且是前端的新手。 我做了一些搜索,但是找不到解決方案。

感謝您的幫助。

使用CSS垂直居中可能會很痛苦。 看看Dead Center 它需要一個額外的容器“水平”來知道垂直中心在哪里,不幸的是,您必須知道要居中的內容的尺寸,以便可以偏移它。

像這樣...

 body { margin: 0px } #horizon { text-align: center; position: absolute; top: 50%; left: 0px; width: 100%; height: 1px; overflow: visible; visibility: visible; display: block } #content { margin-left: -125px; position: absolute; top: -35px; left: 50%; width: 250px; height: 70px; visibility: visible } 
 <body> <div id="horizon"> <div id="content"> content you want centered </div> </div> </body> 

 .centered { background-color: red; width:50px; height:50px; position: fixed; top: 50%; left: 50%; /* bring your own prefixes */ transform: translate(-50%, -50%); } 
 <div class="centered"> 

這在Chrome 38上不再能正常工作。嘗試在上方加載死點網站並調整瀏覽器的大小-請查看文字中的變形。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM