0 Comments

CSS中实现DIV容器垂直居中方法揭秘(5)

发布于:2013-08-09  |   作者:广州网站建设  |   已聚集:人围观

五、完美的解决方案

那么我们综合上面两种方法就可以得到一个完美的解决方案,不过这要用到CSShack的知识。

ExampleSourceCode


  1. DIV#wrap{  
  2. display:table;  
  3. border:1pxsolid#FF0099;  
  4. background-color:#FFCCFF;  
  5. width:760px;  
  6. height:400px;  
  7. _position:relative;  
  8. overflow:hidden;  
  9. }  
  10. DIV#subwrap{  
  11. vertical-align:middle;  
  12. display:table-cell;  
  13. _position:absolute;  
  14. _top:50%;  
  15. }  
  16. DIV#content{  
  17. _position:relative;  
  18. _top:-50%;  
  19. }  

至此,一个完美的居中方案就产生了。
广州网站建设,网站建设,广州网页设计,广州网站设计

SourceCodetoRun


  1. <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN"  
  2. "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
  3. <htmlxmlnshtmlxmlns="http://www.w3.org/1999/xhtml"> 
  4. <head> 
  5. <title>52CSS.com</title> 
  6. <metahttp-equivmetahttp-equiv="Content-Type"content="text/html;charset=utf-8"/> 
  7. <styletypestyletype="text/css"> 
  8.  body{font-size:12px;font-family:tahoma;}  
  9.  DIV#wrap{  
  10.   display:table;  
  11.   border:1pxsolid#FF0099;  
  12.   background-color:#FFCCFF;  
  13.   width:760px;  
  14.   height:400px;  
  15.   _position:relative;  
  16.   overflow:hidden;  
  17.  }  
  18.  DIV#subwrap{  
  19.   vertical-align:middle;  
  20.   display:table-cell;  
  21.   _position:absolute;  
  22.   _top:50%;  
  23.  }  
  24.  DIV#content{  
  25.   _position:relative;  
  26.   _top:-50%;  
  27.  }  
  28. </style> 
  29. </head> 
  30.  
  31. <body> 
  32. <DIVidDIVid="wrap"> 
  33.  <DIVidDIVid="subwrap"> 
  34.   <DIVidDIVid="content"><pre>现在我们要使这段文字垂直居中显示!  
  35.  DIV#wrap{  
  36.   border:1pxsolid#FF0099;  
  37.   background-color:#FFCCFF;  
  38.   width:760px;  
  39.   height:500px;  
  40.   position:relative;  
  41.  }  
  42.  DIV#subwrap{  
  43.   position:absolute;  
  44.   border:1pxsolid#000;  
  45.   top:50%;  
  46.  }  
  47.  DIV#content{  
  48.   border:1pxsolid#000;  
  49.   position:relative;  
  50.   top:-50%;  
  51.  }</pre> 
  52.   </DIV> 
  53.  </DIV> 
  54. </DIV> 
  55. </body> 
  56. </html> 
  57.  

[可先修改部分代码再运行查看效果]

垂直居中vertical-align的值是middle,而水平居中align的值是center,虽然同是居中但关键字不同。
广州网站建设,网站建设,广州网页设计,广州网站设计

飞机