1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
<!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=utf-8" />
<title>未知高度的图片垂直居中</title>
<style type="text/css">
*{ margin:0; padding:0;}
#box{
width:500px;height:400px;
text-align:center;
border:1px solid #d3d3d3;background:#fff;

display: table;
*display:block;
*font-size:349px; // 字体大小约为容器高度的0.873倍 400*0.873 = 349
*font-family:Arial; // 防止非utf-8引起的hack失效问题,如gbk编码
}

#box span{
display: table-cell; vertical-align:middle;
}
#box img{
*vertical-align:middle;
}
</style>
</head>

<body>
<div id="box">
<span><img src="http://www.xxx.com/resource/images/extend/logo/logo_hd.png" width="420" height="300" alt="" /></span>
</div>
</body>
</html>