RSS
热门关键字:  CSS教程  CSS布局  CSS样式  CSS hack  CSS JS
当前位置 :| 主页>CSS布局>

让div居中对齐

来源:cssplay 作者:cssplay 时间:2008-02-28 点击:

使用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"> 
<html xmlns="http://www.w3.org/1999/xhtml" lang="zh-cn">  
<head>  
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />  
<title>居中div演示效果</title>  
<style type="text/css">  
.align-center{   
    margin:0 auto;      /* 居中 这个是必须的,,其它的属性非必须 */   
    width:500px;        /* 给个宽度 顶到浏览器的两边就看不出居中效果了 */   
    background:red;     /* 背景色 */   
    text-align:center;  /* 文字等内容居中 */   
}   
</style>  
</head>  
  
<body>  
<div class="align-center">居中div演示效果</div>  
</body>  
</html> 
<!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" lang="zh-cn">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>居中div演示效果</title>
<style type="text/css">
.align-center{
 margin:0 auto;  /* 居中 这个是必须的,,其它的属性非必须 */
 width:500px;  /* 给个宽度 顶到浏览器的两边就看不出居中效果了 */
 background:red;  /* 背景色 */
 text-align:center;  /* 文字等内容居中 */
}
</style>
</head>
<body>
<div class="align-center">居中div演示效果</div>
</body>
</html>

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

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

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


Tag:div居中   对齐  
最新评论共有 0 位网友发表了评论
发表评论
评论内容:不能超过250字,需审核,请自觉遵守互联网相关政策法规。
用户名: 密码:
匿名?
注册
热点实例