代码一:
示例一
<!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=gb18030" />
<title>Untitled Document</title>
<style type="text/css">
a{
/* 以下行在标准浏览器必需,IE6可选 */
display:table-cell;
vertical-align:middle;
width:300px;
height:300px;
/* 以下可选 */
text-align:center;
border:1px solid #CCC;
}
img{
border:1px dotted #999;
}
</style>
<!--[if lte IE 7]>
<style type="text/css">
a{
line-height:300px; /* 与 a 的高度相同 */
display:block; /* 不支持 table-cell,用 block 替代,否则不能水平居中 */
}
img{
vertical-align:middle;
}
span{
display:inline-block;
}
</style>
<![endif]-->
</head>
<body>
<a href="#" /><img src="http://img.baidu.com/img/img-jg.gif" /><span></span></a>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030" />
<title>Untitled Document</title>
<style type="text/css">
a{
/* 以下行在标准浏览器必需,IE6可选 */
display:table-cell;
vertical-align:middle;
width:300px;
height:300px;
/* 以下可选 */
text-align:center;
border:1px solid #CCC;
}
img{
border:1px dotted #999;
}
</style>
<!--[if lte IE 7]>
<style type="text/css">
a{
line-height:300px; /* 与 a 的高度相同 */
display:block; /* 不支持 table-cell,用 block 替代,否则不能水平居中 */
}
img{
vertical-align:middle;
}
span{
display:inline-block;
}
</style>
<![endif]-->
</head>
<body>
<a href="#" /><img src="http://img.baidu.com/img/img-jg.gif" /><span></span></a>
</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=gb18030" />
<title>Untitled Document</title>
<style type="text/css">
div.outer{
width:300px;
height:300px;
/* 以下可选 */
text-align:center;
border:1px solid #CCC;
}
span{
display:inline-block;
line-height:300px;
width:0px;
visibility:hidden;
}
div.inner{
display:inline-block;
*display:inline;
vertical-align:middle;
}
img{ border:1px dotted #CCC; }
</style>
</head>
<body>
<div class="outer">
<span>.</span><div class="inner"><img src="http://img.baidu.com/img/img-jg.gif" /></div>
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030" />
<title>Untitled Document</title>
<style type="text/css">
div.outer{
width:300px;
height:300px;
/* 以下可选 */
text-align:center;
border:1px solid #CCC;
}
span{
display:inline-block;
line-height:300px;
width:0px;
visibility:hidden;
}
div.inner{
display:inline-block;
*display:inline;
vertical-align:middle;
}
img{ border:1px dotted #CCC; }
</style>
</head>
<body>
<div class="outer">
<span>.</span><div class="inner"><img src="http://img.baidu.com/img/img-jg.gif" /></div>
</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=gb18030" />
<title>Untitled Document</title>
<style type="text/css">
.box {
/*非IE的主流浏览器识别的垂直居中的方法*/
display: table-cell;
vertical-align:middle;
/*设置水平居中*/
text-align:center;
/* 针对IE的Hack */
*display: block;
*font-size: 175px;/*约为高度的0.873,200*0.873 约为175*/
*font-family:Arial;/*防止非utf-8引起的hack失效问题,如gbk编码*/
width:200px;
height:200px;
border: 1px solid #eee;
}
.box img {
/*设置图片垂直居中*/
vertical-align:middle;
}
</style>
</head>
<body>
<div class="box">
<img src="http://img.baidu.com/img/img-jg.gif" />
</div>
</body>
</html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb18030" />
<title>Untitled Document</title>
<style type="text/css">
.box {
/*非IE的主流浏览器识别的垂直居中的方法*/
display: table-cell;
vertical-align:middle;
/*设置水平居中*/
text-align:center;
/* 针对IE的Hack */
*display: block;
*font-size: 175px;/*约为高度的0.873,200*0.873 约为175*/
*font-family:Arial;/*防止非utf-8引起的hack失效问题,如gbk编码*/
width:200px;
height:200px;
border: 1px solid #eee;
}
.box img {
/*设置图片垂直居中*/
vertical-align:middle;
}
</style>
</head>
<body>
<div class="box">
<img src="http://img.baidu.com/img/img-jg.gif" />
</div>
</body>
</html>