您现在的位置:软界网技术中心WEB开发 > 技术显示
让div居中对齐
2008-1-17 20:41:39   网友评论       阅读次数 点此评论
   

使用margin-left:auto;margin-right:auto; 可以让你的div居中对齐。
.style{margin-left:auto;margin-right:auto;}
缩写形式为:
.style{margin:0 auto;}
数字0 表示上下边距是0。可以按照需要设置成不同的值。

查看下面的例子:
  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <htmlxmlns="http://www.w3.org/1999/xhtml"lang="zh-cn">
  3. <head>
  4. <metahttp-equiv="Content-Type"content="text/html;charset=UTF-8"/>
  5. <title>居中div演示效果</title>
  6. <styletype="text/css">
  7. .align-center{
  8. margin:0auto;/*居中这个是必须的,,其它的属性非必须*/
  9. width:500px;/*给个宽度顶到浏览器的两边就看不出居中效果了*/
  10. background:red;/*背景色*/
  11. text-align:center;/*文字等内容居中*/
  12. }
  13. </style>
  14. </head>
  15. <body>
  16. <divclass="align-center">居中div演示效果</div>
  17. </body>
  18. </html>

软件开发网 www.mscto.com


注意,需要加上上面的那句

才能生效,要是不想要这句的话,也可以给body加一个属性:
body{text-align:center;}

另外,让div内的内容(包括文字及图片)居中的代码是: text-align:center;

 
      来源: 作者:
 
【评论查看】