未知寬高圖片在元素中垂直水平居中程式碼例項

螞蟻小編發表於2017-04-09

分享一段程式碼例項,它實現了未知寬高的圖片在指定元素中垂直水平居中效果。

程式碼例項如下:

[HTML] 純文字檢視 複製程式碼
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<meta name="author" content="http://www.softwhy.com/" />
<title>螞蟻部落</title>
<style type="text/css">
* {
  padding: 0;
  margin: 0;
}
#antzone{
  display:table-cell;
  *display:block;
  *position:relative;
  width:200px;
  height:200px;
  text-align:center;
  vertical-align:middle;
  background:blue;
}
#antzone p{
  *position:absolute;
  *top:50%;
  *left:50%;
  margin:0;
}
#antzone p img{
  *position:relative;
  *top:-50%;
  *left:-50%;
  vertical-align:middle;
}
</style>
</head>
<body>
<div id="antzone">
  <p><img src="demo/CSS/img/100x100.png"></p>
</div>
</body>
</html>

相關文章