使用CSS让DIV水平垂直居中的方法。

Code
<html>
<head>
<title>DIV水平垂直居中</title>
<style>
body
{font-size:12px; margin:0; padding:0;}
.div1
{position:absolute;top:50%;left:50%;width:500px;height:300px; margin-top:-150px; margin-left:-250px;
border:1px #999 solid; background:#CEE7FF;}
</style>
</head>
<body>
<div class="div1">已经水平垂直居中了</div>
</body>
</html>

要点:

1.position属性设置为:absolute

2.top和left属性都设为:50%

3.margin-top宽度设置为height高度的一半,margin-left属性设置为width宽度的一半。

posted on 2009-12-01 19:03  暮光青年  阅读(2750)  评论(5编辑  收藏  举报

导航