|
|
|
|
|
利用CSS使Div水平垂直居中http://www.xishui.net 2008年07月16日23:14 浠水网
通常在制作网站首页时需要使页面内容在水平和垂直方向上都居中。表格布局时代常用的方法是内容之前添加换行。实际上通过CSS有更方便的办法。 这个方法出自creamu.com。先来看看代码和效果吧。
《style>
#warp {
position: absolute;
width:500px;
height:200px;
left:50%;
top:50%;
margin-left:-250px;
margin-top:-100px;
border: solid 3px red;
}
《/style>
《body>
《div id=warp>Test《/div>
《/body>
![]() 原理很简单:将left和top设置为50%来定位div到浏览器中央,再将margin-left和margin-top值设置为宽和高的一半,使div居中显示。 《html> 《style> #center { background: #090; width: 100px; height: 96px; position: relative; left: 50%; margin-left: expression(-(this.offsetWidth/2)+"px"); margin-top:20%; } 《/style> 《div id='center'> 《/div>
【发表评论】
|
|||||||||||