水平居中的两种方法margin text-align

<!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=utf-8" />
<title>无标题文档</title>
</head>
<style type="text/css">
/*
第一种方法是使用margin:0 auto方法
*/
#demo1 { width: 50%; margin: 0 auto; background: red; }

/*
  第二种使用的是text-align:center方法,此方法只能对行级元素起作用,所以要先设置display的属性
*/
#demo2 { text-align: center; } #demo2_1 { display: inline-block; width: 50%; background: red; } </style> <body> demo1 <div id="demo1">demo1</div> demo2 <div id="demo2"> <div id="demo2_1">demo2</div> </div> </body> </html>

 

posted @ 2015-12-29 22:07  myherodream  阅读(347)  评论(0编辑  收藏  举报