<!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">
.box {
/*非IE的主流浏览器识别的垂直居中的方法*/
display: table-cell;
vertical-align:middle;
/*设置水平居中*/
text-align:center;
/* 针对IE的Hack */
*display: block;
*font-size:262px;/*约为高度的0.873,300*0.873 约为262*/
*font-family:Arial;/*防止非utf-8引起的hack失效问题,如gbk编码*/
width:400px;
height:300px;
border:1px solid #eee;
}
.box img {
/*设置图片垂直居中*/
vertical-align:middle;
}
</style>
</head>
<body>
<div class="box">
<img alt="dd" src="http://www.baidu.com/img/baidu_sylogo1.gif" />
</div>
</body>
</html>