div垂直居中 css div盒子上下垂直居中

 
 
创建时间:2015年5月24日(星期天) 下午3:46 | 分类:工作生活 | 字数:459  | 另存为... | 打印 | 添加到日历
  1. <!DOCTYPE html> 
  2. <html> 
  3. <head> 
  4. <meta charset="utf-8" /> 
  5. <title>上下垂直居中 在线演示 DIVCSS5</title> 
  6. <style> 
  7. #main {position: absolute;400px;height:200px;left:50%;top:50%; 
  8. margin-left:-200px;margin-top:-100px;border:1px solid #00F} 
  9. /*css注释:为了方便截图,对CSS代码进行换行*/ 
  10. </style> 
  11. <body> 
  12. <div id="main">DIV水平居中和上下垂直居中</div> 
  13. </body> 
  14. </html> 
 
这里设置一个“#main”对象样式,400px,200px,使用了绝对定位position样式同时使用绝对定位left和top,并且同时设置margin-topmargin-left,为了观察到效果,所以对此div盒子加了个红色边框

原文地址:https://www.cnblogs.com/xtmp/p/6262075.html