未知高度的图片垂直居中显示
在HTML代码中对未知高度的图片进行垂直居中显示
操作方法
- 01
方法一: <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>图片垂直居中</title><style>.box{ width:800px; margin:0 auto; height:600px;border:2px solid #000; text-align:center;}span{ display:inline-block; height:100%; vertical-align:middle;}img{ vertical-align:middle;}</style></head><body><h3><center>图片垂直居中一</center></h3><div class="box"> <img src="http://img03.taobaocdn.com/imgextra/i3/263817957/TB2Wqr5apXXXXbnXXXXXXXXXXXX-263817957.jpg" /><span></span></div></body></html>
- 02
方法二: <!DOCTYPE HTML><html><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8"><title>图片垂直居中</title><style>.box{ width:800px; margin:0 auto; height:600px;border:2px solid #000;display:table;position:relative; overflow:hidden;}span{ display:table-cell; text-align:center; vertical-align:middle;*position:absolute;left:50%;top:50%;}img{ *position:relative; vertical-align:top;left:-50%;top:-50%;}</style></head><body><h3><center>图片垂直居中二</center></h3><div class="box"> <span><img src="http://img03.taobaocdn.com/imgextra/i3/263817957/TB2Wqr5apXXXXbnXXXXXXXXXXXX-263817957.jpg" /></span></div></body></html>