DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> CSS詳解
單行圖片與文字垂直居中的實戰解析
編輯:CSS詳解     

先用表格實現設計效果

<style type="text/CSS">
<!--
body{
  font-size:12px;
  font-family:"宋體";
}
table{
  border:1px solid #E6EFF8;
  margin-bottom:2px;
}
td{
  height:23px;
  line-height:23px;
  padding:0 3px;
}
.name{
  font-weight:bold;
}
.money{
  font-size:18px;
  color:#FFA200;
  font-weight:bold;
}
.money strong{
  font-size:14px;
}
.renovate{
  color:#707070;
}
-->
</style>
<table width="380" cellspacing="0">
 <tr>
  <td width="80" class="name">遨游旅行網</td>
  <td width="157" class="renovate">最後更新:1小時以前 <img src="images/ico/ico_time_bak.gif" alt="time" width="12" height="12" /></td>
  <td width="56" class="money">368<strong>元</strong></td>
  <td width="77"><img src="images/btn/btn_speak_for.gif" alt="訂票" width="61" height="17" /></td>
 </tr>
</table>

IE、Firefox 顯示效果

在表格中實現垂直居中很容易,但接下來改用 DIV 卻沒那麼簡單——垂直居中向來是 DIV 布局裡最令人頭疼的一大難題。

用 DIV 模擬上面的表格效果

<style type="text/CSS">
<!--
.simulate_table {
}
.simulate_table .row {
  width:378px;
  border:1px solid #E6EFF8;
  float:left;
}
.simulate_table .col1, .simulate_table .col2, .simulate_table .col3, .simulate_table .col4 {
  float:left;
  width:78px;
  padding-left:3px;
  line-height:23px;
  height:23px;
}
.simulate_table .col2 {
  width:152px;
}
.simulate_table .col3 {
  width:60px;
}
.simulate_table .col4 {
  width:77px;
  padding-left:0;
}
-->
</style>
<div class="simulate_table">
 <div class="row">
  <div class="col1"><span class="name">遨游旅行網</span></div>
  <div class="col2"><span class="renovate">最後更新:1小時以前 <img src="images/ico/ico_time_bak.gif" alt="time" width="12" height="12" /></span></div>
  <div class="col3"><span class="money">368<strong>元</strong></span></div>
  <div class="col4"><img src="images/btn/btn_speak_for.gif" alt="訂票" width="61" height="17" /> </div>
 </div>
</div>

Firefox、IE7 顯示效果

IE6 顯示效果

分析

從效果圖不難看出,Firefox 在垂直方向上的支援也並不完美:若一行中只有文字,只要寫上等值的行高與高度,它就會垂直居中;第二種情況是文字與圖片並存,同樣能居中(圖片底部落在文字的基線上,可以試著把圖片高度加大,效果會更明顯);而 IE6 則會把圖文全部頂到上方。第三種情況是只有圖片,此時 Firefox 與 IE6 都會頂到最上方。

之所以沒單獨分析 IE7,是因為它的表現與 Firefox 相同。

那麼,該如何解決這種瀏覽器不相容的問題呢?可以沿用《單行圖片文字垂直居中問題》一文中的思路來逐一破解。仔細觀察這張設計圖的特徵:第一列與第三列只有文字,所以只要寫上等值的行高與高度,就能解決這兩列的垂直居中;第二列可以為 IE6 單獨寫一個樣式,給其中的圖片加上適當的上邊距(或上補白),或者直接給這一列的 DIV 加上上補白;第四列在 IE 和 Firefox 中都會頂到上方,那就為它們寫入上補白,或給圖片加上上边距/補白。

當然,還有一種思路,就是分別為每一列都寫入上補白,這同樣值得一試。

以上是依據設計特點來撰寫樣式的一種思路,希望拋磚引玉,能給你帶來一些啟發,而非放諸四海皆準的萬能標準。

最後附上調整後的樣式:

.simulate_table .row {
  width:378px;
  border:1px solid #E6EFF8;
  float:left;
}
.simulate_table .col1, .simulate_table .col2, .simulate_table .col3, .simulate_table .col4 {
  float:left;
  width:78px;
  padding-left:3px;
}
.simulate_table .col1, .simulate_table .col2, .simulate_table .col3 {
  line-height:23px;
  height:23px;
}
.simulate_table .col2 {
  width:152px;
}
.simulate_table .col3 {
  width:60px;
}
.simulate_table .col4 {
  width:77px;
  padding-left:0;
  padding-top:3px;
}
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved