1. 程式人生 > >html 中div垂直居中的三種方式

html 中div垂直居中的三種方式

1.第一種:
  1. <style type="text/css">   
  2. <!--   
  3. .con_div{   
  4. width:400px;   
  5. height:300px;   
  6. border:1px solid #777;   
  7. text-align:center;   
  8. display:table-cell;   
  9. vertical-align:middle;   
  10. background:red;   
  11. color:#fff;   
  12. line-height:300px;   
  13. }   
  14. -->   
  15. </style>   
  16. <div class="con_div">   
  17. 測試內容ddddddddddd   
  18. </div>  

2.第二種:

  1. <style type="text/css">   
  2. <!--   
  3. .con_div{   
  4. width:400px;   
  5. height:300px;   
  6. border:1px solid #777;   
  7. text-align:center;   
  8. display:table-cell;   
  9. vertical-align:middle;   
  10. background:red;   
  11. color:#fff   
  12. }   
  13. /*FOR IE*/
  14. .fixie{   
  15. width:0;   
  16. height:100%;   
  17. display:inline-block;   
  18. vertical-align:middle;   
  19. }   
  20. -->   
  21. </style>   
  22. <div class="con_div">   
  23. <span class="fixie"></span>   
  24. 測試內容   
  25. </div>

3.第三種:

  1. <style>
  2. #warp {   
  3.   position: absolute;   
  4.   width:500px;   
  5.   height:200px;   
  6.   left:50%;   
  7.   top:50%;   
  8.   margin-left:-250px;   
  9.   margin-top:-100px;   
  10.   border: solid 3px red;   
  11. }   
  12. </style>
  13. <body>
  14. <divid=warp>Test</div>
  15. </body>