在Firefox,IE6和IE7中可以正常使用的将HTML内容垂直和水平居中的解决方案是什么?
一些细节:
换句话说,屏幕的宽度应表示为:
“leftSpace width =(screenWidth-widthOfCenteredElement)/2” +
“centeredElement width = widthOfCenteredElement” +
“rightSpace宽度=(screenWidth-widthOfCenteredElement)/2”
和高度相同:
“topSpace高度=(screenHeight-heightOfCenteredElement)/2” +
“centeredElement height = heightOfCenteredElement” +
“bottomSpace高度=(screenWidth-heightOfCenteredElement)/2”
最佳答案
从http://www.webmonkey.com/codelibrary/Center_a_DIV
#horizon
{
text-align: center;
position: absolute;
top: 50%;
left: 0px;
width: 100%;
height: 1px;
overflow: visible;
display: block
}
#content
{
width: 250px;
height: 70px;
margin-left: -125px;
position: absolute;
top: -35px;
left: 50%;
visibility: visible
}
<div id="horizon">
<div id="content">
<p>This text is<br><emphasis>DEAD CENTRE</emphasis ><br>and stays there!</p>
</div><!-- closes content-->
</div><!-- closes horizon-->