原文地址 http://www.cnblogs.com/rubylouvre/archive/2010/07/08/1774025.html

 

 

代码
<!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=gb2312" /> 
    
<title>司徒正美 CSS垂直居中</title>  
    
<style type="text/css">  
      .container
{  
        width
:500px;/*装饰*/
        height
:500px;  
        background
:#B9D6FF;  
        border
: 1px solid #CCC;  
      
}  
      
    
</style>  
  
</head>  
  
<body>  
    
<h1>垂直居中(table)</h1>  
    
<div class='container'>
        
<table width="100%" height="100%">
            
<tr>
               
<td align="center" valign="middle">
                  
<img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
               
</td>
           
</tr>
       
</table> 
   
</div>
    
  
</body>  
</html> 

----------------------------------------
<!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=gb2312" />
<style type="text/css">
.container 
{
  width
:500px;
  height
:500px;
  line-height
:500px;
  background
:#B9D6FF url(http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg)  no-repeat center center;
  border
:1px solid #f00;
  text-align
: center;
}

</style>

</head>
<body>
<h1>垂直居中(背景图片法)</h1>
<div class="container"></div>
</body>
</html>
--------------------------------------------------
<!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=gb2312" />
<style type="text/css">  
      .container
{  
        
/*IE8与标准游览器垂直对齐*/
        display
: table-cell;
        vertical-align
:middle; 
        width
:500px;/*装饰*/
        height
:500px;  
        background
:#B9D6FF;  
        border
: 1px solid #CCC;  
      
}  
      .container img
{  
        display
:block;/*让其具备盒子模型*/
        margin
:0 auto;  
        text-align
:center;
        margin-top
:expression((500 - this.height )/2);/*让IE567垂直对齐 */
      
}  
    
</style>  
  
</head>  
  
<body>  
    
<h1>垂直居中(CSS表达式)</h1>  
    
<div class="container">  
      
<img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />  
    
</div>  
  
</body>  
</html> 
----------------------------------------
<!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=gb2312" />
<title>司徒正美 CSS垂直居中</title>
    
<style type="text/css">
      div 
{
       
/*IE8与标准游览器垂直对齐*/
        display
:table-cell;
        vertical-align
:middle;
        overflow
:hidden;
        position
:relative;
        text-align
:center;
        width
:500px;/*装饰*/
        height
:500px;
        border
:1px solid #ccc;
        background
:#B9D6FF;
      
}
      div p 
{
        +position
:absolute;
        top
:50%
      
}
      img 
{
        +position
:relative;
        top
:-50%;
        left
:-50%;
      
}
 
    
</style>
  
</head>
  
<body>
    
<h1>垂直居中(绝对定位)</h1>
    
<div class="container">
      
<p>
        
<img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
      
</p>
    
</div>
  
</body>
</html>

------------------------------------------------
<!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=gb2312" />
<style type="text/css">
      div
{
        width
:500px;
        height
:500px;
        line-height
:500px;
        text-align
:center;
        background
:#B9D6FF;
        border
:1px solid #f00;
      
}
      div span
{
        height
:100%\9;
        writing-mode
:tb-rl\9;
      
}
      div img
{
        vertical-align
:middle
      
}
    
</style>


  
</head>
  
<body>
    
<h1>垂直居中(writing-mode)</h1>
    
<div class="container">
      
<span>
        
<img src="http://images.cnblogs.com/cnblogs_com/rubylouvre/205314/r_iebug.jpg" />
      
</span>
    
</div>
  
</body>
</html>