【html】【10】div布局[div水平垂直居中]

必看参考:

http://www.jb51.net/css/28259.html

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

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

也可以给body加一个属性: body{text-align:center;} 
另外,让div内的内容(包括文字及图片)居中的代码是: text-align:center; 

(水平垂直居中)

 1 <title>水平垂直居中div演示效果</title> 
 2 <style type="text/css"> 
 3 .align-center{ 
 4    position:fixed;left:50%;top:50%;margin-left:width/2;margin-top:height/2;
 5 } 
 6 </style> 
 7 </head> 
 8 <body> 
 9 <div class="align-center">水平垂直居中div演示效果</div> 
10 </body> 
11 </html> 

ok

原文地址:https://www.cnblogs.com/aiqingqing/p/5032162.html