热门文章 | 热门软件| 热门源码 | 热门电影 | 知识库 | 联系我们
软件 源码 教程 影视 健康 招聘
  HTML | JavaScript | ASP | PHP | JSP | NET | VB | VC | VF | Windows | Linux | Mysql | Mssql | Oracle | Struts 
当前位置: 创世纪计算机资源网 -> 文章频道 ->css 
站内搜索:
让div居中对齐
作者:佚名 来源:div-css.com 整理日期:2007-9-27

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

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

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

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

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

相关文章