DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
任意尺寸圖片垂直居中的三種方法(兼容性都不錯)
編輯:布局實例     

需要垂直居中的圖片高度還不確定,這是布局中頗具挑戰的一種情況。下面三種方法我都實測過,除第一種僅限標準瀏覽器外,另外兩種的兼容表現都不錯。

方法一:display:table-cell

把外層容器的顯示模式設為 table,再給 img 外面嵌套一個 span 並設為 table-cell。這樣 span 內部就具備了表格單元格的行為,可以直接用 vertical-align 對齊內容。

<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>方法1 - 未知高度的圖片垂直居中</title>
<style type="text/css">
body { height:100%; }
#box{
  width:500px; height:400px;
  display:table;
  text-align:center;
  border:1px solid #d3d3d3;
  background:#fff;
}
#box span{
  display:table-cell;
  vertical-align:middle;
}
#box img{ border:1px solid #ccc; }
</style>
<!--[if lte IE 7]>
<style type="text/css">
#box{ position:relative; overflow:hidden; }
#box span{ position:absolute; left:50%; top:50%; }
#box img{ position:relative; left:-50%; top:-50%; }
</style>
<![endif]-->
</head>
<body>
<div id="box">
  <span><img src="images/demo_zl.png" alt="" /></span>
</div>
</body>
</html>

針對 IE6/IE7 用了條件注釋:採用「父層 50% 定位 + 子層負 50% 回拉」的經典變通,讓元素中心對齊容器中心。

方法二:空標簽撐高度

標準瀏覽器部分與上面相同,差別在於針對 IE 在 img 前插入一對空 i 標簽。

<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>方法2 - 未知高度的圖片垂直居中</title>
<style type="text/css">
body{ height:100%; }
#box{
  width:500px;
  height:400px;
  display:table-cell;
  text-align:center;
  vertical-align:middle;
  border:1px solid #d3d3d3;
  background:#fff;
}
#box img{ border:1px solid #ccc; }
</style>
<!--[if IE]>
<style type="text/css">
#box i {
  display:inline-block;
  height:100%;
  vertical-align:middle;
}
#box img { vertical-align:middle; }
</style>
<![endif]-->
</head>
<body>
<div id="box">
  <i></i><img src="logo.png" alt="" />
</div>
</body>
</html>

原理很巧:那個空 i 標簽高度撐滿容器並垂直居中,img 與它同行、同樣 middle 對齊,於是就被帶到了中間。

方法三:偽類加 CSS 表達式

img 外包裹一層 p,標準瀏覽器用 p 的 :before 偽類實現居中,IE6/IE7 則用 CSS 表達式補全。

<html xmlns="https://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>方法3 - 未知高度的圖片垂直居中</title>
<style type="text/css">
body { height:100%; }
#box{
  width:500px; height:400px;
  text-align:center;
  border:1px solid #d3d3d3;
  background:#fff;
}
#box p{
  width:500px; height:400px;
  line-height:400px; /* 行高等於高度 */
}
/* 兼容標準瀏覽器 */
#box p:before{
  content:".";
  margin-left:-5px;
  font-size:10px;    /* 修正居中的小偏差 */
  visibility:hidden;
}
#box p img{
  *margin-top:expression((400 - this.height)/2); /* 兼容 IE6/IE7 */
  vertical-align:middle;
  border:1px solid #ccc;
}
</style>
</head>
<body>
<div id="box">
  <p><img src="images/demo_zl.png" alt="" /></p>
</div>
</body>
</html>

實測前兩種沒有問題,第三種在某些環境下表現不夠穩定。

今天的首選

新項目直接用 Flexbox,容器三行即可,且不關心圖片尺寸:

#box{
  width:500px; height:400px;
  display:flex;
  align-items:center;
  justify-content:center;
}

或者更簡潔的 Grid:

#box{ display:grid; place-items:center; }

上面三種方法的價值在於理解原理——vertical-align 的基線機制、行高對垂直位置的影響,這些知識在處理複雜排版時依然用得上。

XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved