最近布局时遇到了一个问题,就是如何设置div居中?通过查找资料终于找到了解决方案,只需要把要居中的div外嵌套一个div,设置其“text-align:center;”,然后设置要居中的div为“margin-left:auto;margin-right:auto;”
提示:若设置后IE5中仍不居中,只需将外层div设置为“100%;”,即可解决问题。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>设置div居中(兼容IE5+)</title>
<style type="text/css">
.content{width:100%;height:200px;text-align:center;}
.center{width:500px;height:200px;margin-left:auto;margin-right:auto;border:1px #999 dashed;}
</style>
</head>
<body>
<div class="content">
<div class="center"></div>
</div>
</body>
</html>
