网页居中

我看到有些网页在IE里是居中的,可是到了Firefox里就靠左了,其实这个问题很简单,看下面这段代码:

<!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=gb2312" />
<title>网页居中</title>
<style>
        body
        {
                text-align:center;
        }
       
        div.window
        {
                border:#330066 solid 1px;
                800px;
                height:700px;
                margin-left:auto;
                margin-right:auto;
        }
       
</style>
</head>

<body>
        <div class="window"></div>
</body>
</html>

复制上面的代码,保存为index.html,用浏览器浏览下效果,可以看到窗口永远是居中的
原理是在你要居中的窗口(该例是window)的样式中添加margin-left:auto;margin-right:auto;让它左右空隙都自动,这样它就能自动居中对齐了
作者:观海看云个人开发历程知识库 - 博客园
出处:http://www.cnblogs.com/zhangtao/
文章版权归本人所有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。
原文地址:https://www.cnblogs.com/zhangtao/p/1663979.html