XML/HTML代码
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <style type="text/css">
- * {margin:0;padding:0;}
- div {
- width:500px;
- height:500px;
- border:1px solid #ccc;
- overflow:hidden;
- position:relative;
- display:table-cell;
- text-align:center;
- vertical-align:middle;
- }
- div p {
- position:absolute;
- top:50%;
- }
- img {
- position:relative;
- top:-50%;left:-50%;
- width:276px;
- height:110px;
- }
- div>p,p>img {
- position:static;
- }
- </style>
- <div><p><img src="http://www.google.com/intl/en/images/logo.gif" /></p></div>
标准浏览器下另类方法:
XML/HTML代码
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <style type="text/css">
- body {
- margin:0;padding:0
- }
- div {
- width:500px;
- height:500px;
- line-height:500px;
- border:1px solid #ccc;
- overflow:hidden;
- position:relative;
- text-align:center;
- margin:auto
- }
- div p {
- position:absolute;
- top:50%
- }
- img {
- position:relative;
- top:-50%;left:-50%;
- width:276px;
- height:110px
- }
- div>p,p>img {
- position:static;
- vertical-align:middle
- }
- p:after {
- content:".";font-size:1px;
- visibility:hidden
- }
- </style>
- <div><p><img src="http://www.google.com/intl/en/images/logo.gif"/></p></div>
标准浏览器严格DTD下:
XML/HTML代码
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <style type="text/css">
- body {
- margin:0;padding:0
- }
- div {
- width:500px;
- height:500px;
- line-height:500px;
- border:1px solid #ccc;
- overflow:hidden;
- position:relative;
- text-align:center;
- margin:auto
- }
- img {
- position:relative;
- top:-50%;left:-50%;
- width:276px;
- height:110px
- }
- div p {
- position:absolute;
- top:50%
- }
- div p,p>img {
- position:static;
- vertical-align:middle
- }
- </style>
- <div><p><img src="http://www.google.com/intl/en/images/logo.gif" /></p></div>
标准浏览器严格DTD下:
XML/HTML代码
- <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
- <style type="text/css">
- body {
- margin:0;padding:0
- }
- div {
- width:500px;
- height:500px;
- line-height:500px;
- border:1px solid #ccc;
- overflow:hidden;
- position:relative;
- text-align:center;
- margin:auto
- }
- img {
- position:relative;
- top:-50%;left:-50%;
- width:276px;
- height:110px
- }
- div p {
- position:absolute;
- top:50%
- }
- div p,p>img {
- position:static;
- vertical-align:middle
- }
- </style>
- <div><p><img src="http://www.google.com/intl/en/images/logo.gif" /></p></div>
最简单当然是背景图片的方法拉
CSS代码
- * {margin:0;padding:0;}
- div {
- width:500px;border:1px solid #f00;
- height:500px;
- background:url("http://www.google.com/intl/en/images/logo.gif") center no-repeat
- }
相关阅读:

