0
A
回答
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
添加position: fixed;
到它的樣式。如果你知道如何將其集中,那麼只需添加它就可以實現。
看看這裏的更多信息:http://www.w3.org/TR/CSS2/visuren.html#choose-position
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的一半高度,並且必須是負值。
你知道汽車財產的利潤率嗎?這爲您提供了靈活的利潤率,您也可以在其他情況下使用。 – 2011-04-06 19:50:42
給出的示例(onehub)最棒的地方在於它使用的方法沒有在任何解決方案中描述過(但...)。他們使用CSS來設置div的顯示屬性來模擬表格的屬性。這種方法雖然有爭議。 – Guttsy 2011-04-06 19:57:07