http://hi.baidu.com/john8412/item/d07f14c55427f366f6c95de5

以下代码都是在IE8,firefox13,chrome24中运行测试的

 

在Web标准中的页面布局是使用Div配合CSS来实现的。这其中最常用到的就是使整个页面水平居中的效果,这是在页面布局中基本,也是最应该首先掌握的知识。不过,还是经常会有人问到这个问题,在这里我简单总结一下使用Div和CSS实现页面水平居中的方法:

  一、margin:auto 0 与 text-aligh:center

  在现代浏览器(如Internet Explorer 7、Firefox、Opera等)实现水平居中的方法很简单,只要设定到左右两侧的空白为自动即可。意即:

#wrap { margin:0 auto;}

  上面这段代码的意思是说使wrap这个div到左右两侧的距离自动设置,上下为0(可以为任意)。请在现代浏览器(如Internet Explorer 7、Firefox、Opera等)中运行现在的代码:

内容居中.html

 
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <title>使用margin:0 auto使页面内容居中</title>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  6. <style type="text/css">  
  7.     div#wrap {  
  8.         width:760px;  
  9.         margin:0 auto;  
  10.         border:1px solid #333;  
  11.         background-color:#ccc;  
  12.     }  
  13. </style>  
  14. </head>  
  15. <body>  
  16. <div id="wrap"> 
  17.   在Firefox等现代浏览器设定页面元素的水平居中,只要指定margin:0 auto;即可 
  18. <pre>  
  19. div#wrap {  
  20. width:760px;  
  21. margin:0 auto; /*这里的0可以任意值*/  
  22. border:1px solid #ccc;  
  23. background-color:#999;  
  24. }  
  25. </pre>  
  26. </div>  
  27. </body>  
  28. </html> 

运行结果:

 

 上面的效果很好。但是这在Internet Explorer 6及改正的版本中是不起作用的,不过幸好它有自己的解决办法。在Internet Explorer中text-align属性是可继承的,即在父元素中设置后在子元素中就默认具有了该属性。因此我们可以在body标签中设置text-align属性值为center,这样页面内所有的元素都会自动居中,同时我们还要加一个hook把页面中的文字变成我们习惯的阅读方式——居左对齐。因此我们要如此来写代码:

 
  
  1. body {text-align:center;}  
  2. #wrap {text-align:left;} 

  这样在Internet Explorer中我们就轻松实现了Div的居中对齐。因此要在所有的浏览器中显示居中的效果,我们就可以这样写我们的代码:

 
  
  1. body { text-align:center; }  
  2. #wrap { text-align:left;  
  3.   margin:0 auto;  
  4. }  

内容居中2.html

 

 
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <title> CSS+Div实现水平居中对齐的页面布局 </title>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  6. <style type="text/css">  
  7. body { text-align:center; }  
  8. div#wrap {  
  9.         text-align:left;  
  10.         width:760px;  
  11.         margin:0 auto;  
  12.         border:1px solid #333;  
  13.         background-color:#ccc;  
  14. }  
  15. </style>  
  16. </head>  
  17. <body>  
  18. <div id="wrap"> 
  19.   在Firefox等现代浏览器设定页面元素的水平居中,只要指定margin:0 auto;即可 
  20. <pre>  
  21. div#wrap {  
  22. width:760px;  
  23. margin:0 auto; /*这里的0可以任意值*/  
  24. border:1px solid #ccc;  
  25. background-color:#999;  
  26.   在Internet Explorer 6 及以下的版本中我们还要做以下的设置: 
  27. body { text-align:center; }  
  28. div#wrap {  
  29. text-align:left;  
  30. }  
  31. </pre>  
  32. </div>  
  33. </body>  
  34. </html> 

 

不过这里有一个前提,就是设置居中的元素要有固定的宽度,比如这里我们设定了为760像素。

假如去掉:

 

 
  
  1. width:760px;  

运行结果如下:

 

二、相对定位与负的边距

  对于wrap进行相对定位,然后使用负的边距抵消偏移量。这种方法比较简单还很容易实现:

 
  
  1. #wrap {  
  2.   position:relative;  
  3.   width:760px;  
  4.   left:50%;  
  5.   margin-left:-380px  

 

 这段代码的意思是,设置wrap的定位是相对于其父元素body标签的,然后将其左边框移动到页面的正中间(也就是left:50%含意);最后我们再从中间位置向左偏移回一半的距离来,这样就实现了水平居中了。

内容居中(相对定位与负的边距).html

 

 
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  2. <html xmlns="http://www.w3.org/1999/xhtml">  
  3. <head>  
  4. <title> CSS+Div实现水平居中对齐的页面布局 </title>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  6. <style type="text/css">  
  7. div#wrap {  
  8.         position:relative;  
  9.         width:760px;  
  10.         left:50%;  
  11.         margin-left:-380px;  
  12.         border:1px solid #333;  
  13.         background-color:#ccc;  
  14. }  
  15. </style>  
  16. </head> 
  17. <body>  
  18. <div id="wrap"> 
  19.   在所有浏览器中都有效的方法: 
  20. <pre>  
  21. div#wrap {  
  22. position:relative;  
  23. width:760px;  
  24. left:50%;  
  25. margin-left:-380px;  
  26. border:1px solid #333;  
  27. background-color:#ccc;  
  28. }  
  29. </pre>  
  30. </div>  
  31. </body>  
  32. </html> 

运行结果:

同样,在设定水平居中前你需要设定一个固定的宽度。

  P.S.究竟选择哪个方法?

  上面两个方法究竟选择哪种方法好呢?在第一种方法中貌似使用了Hack技术,其实并没有,它是中规中矩的Web标准写法,完全符合规范,因此,两个种方法中完全可以随便的选取其中的任一种进行使用,他们不存在CSS hack的问题。

 

三、其它的居中方式

  上面所说的都是设定了具体宽度的情况下水平居中的实现。有时候我们想做一个弹性布局,或者当一个元素处于一个容器中时我们只想让它居中并不想设定一个具体的宽度。其实这并不是真正的居中布局,就像对一个100%长度的元素来说,你说它是居中对齐还是居左对齐呢?所以所有不高宽度的居中都不是真正的居中。这样的设计我们是使用的像元素的padding来设置的,实际中我们是改变了父元素的容器大小:

如我们希望wrap元素长度随窗口而改变,同时又维持居中,我们就可以这样写:

 
  
  1. body {  
  2.   padding:10px 150px;  

  这里,我们只需要保持父元素左右两侧的填充是相等的就可以了。

其它的居中方式.html

 
  
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  2. <html xmlns="http://www.w3.org/1999/xhtml"> 
  3. <head>  
  4. <title> 随浏览器窗口大小而改变的具有弹性的居中布局 </title>  
  5. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
  6. <style type="text/css">  
  7. body {  
  8.     padding:10px 150px;  
  9. }  
  10. div#wrap {  
  11.     border:1px solid #333;  
  12.     background-color:#ccc;  
  13. }  
  14. </style>  
  15. </head>  
  16. <body>  
  17. <div id="wrap"> 
  18.   一种随浏览器窗口大小而改变的具有弹性的居中布局: 
  19. <pre>body {  
  20. padding:10px 150px;  
  21.   这里,我们只需要保持父元素左右两侧的填充是相等的就可以了。 
  22. </pre>  
  23. </div>  
  24. </body>  
  25. </html> 

 

运行结果:

当然这只是“貌似居中”,不过却常常很有用处。

11111111111