DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
CSS 文字換行實測:IE 與 Firefox 的處理差異
編輯:布局實例     

中文天生會換行,英文卻常常不會,尤其是沒有空格的超長字串。有些人會用 OVERFLOW: HIDDEN 硬藏,但這只是把問題遮住,不是解決。以下整理 div 與 table 兩類場景的實測結果。

針對 div

1. IE 瀏覽器

#wrap {
  white-space: normal;
  width: 200px;
}

或者:

#wrap {
  word-break: break-all;
  width: 200px;
}
<div>ddd1111111111111111111111111111111111</div>

效果:可以正常換行。white-space: normal 是遵循標準的寫法,word-break: break-all 則是 IE 的私有擴充。

2. Firefox 瀏覽器

FF 當時沒有理想的換行方案,只能靠隱藏或加上捲軸:

#wrap {
  white-space: normal;
  width: 200px;
  overflow: auto;
}

或者:

#wrap {
  word-break: break-all;
  width: 200px;
  overflow: auto;
}
<div>ddd1111111111111111111111111111111111111111</div>

效果:容器尺寸正常,多出的內容被隱藏或改以捲軸呈現。不加捲軸的視覺通常更好。

針對 table

1. IE:table-layout: fixed

<style>
.tb { table-layout: fixed; }
</style>

<table width="80">
<tr>
<td>www.qpsh.com www.qpsh.com www.qpsh.com</td>
</tr>
</table>

效果:可以換行。

2. IE:table-layout: fixed 搭配 nowrap

<style>
.tb { table-layout: fixed; }
</style>

<table width="80">
<tr>
<td nowrap>abcdefghigklmnopqrstuvwxyz 1234567890</td>
</tr>
</table>

效果:可以換行。

3. IE:百分比寬度 + fixed + nowrap

<style>
.tb { table-layout: fixed; }
</style>

<table width="80">
<tr>
<td width="25%" nowrap>abcdefghigklmnopqrstuvwxyz 1234567890</td>
<td nowrap>abcdefghigklmnopqrstuvwxyz 1234567890</td>
</tr>
</table>

效果:兩個 td 都能正常換行。

4. Firefox:百分比寬度 + fixed + nowrap + div

<style>
.tb { table-layout: fixed; }
.td { overflow: hidden; }
</style>

<table width="80">
<tr>
<td width="25%" nowrap>
<div>abcdefghigklmnopqrstuvwxyz 1234567890</div>
</td>
<td nowrap>
<div>abcdefghigklmnopqrstuvwxyz 1234567890</div>
</td>
</tr>
</table>

這裡單元格寬度務必用百分比定義。效果:正常顯示,但不會換行。

後記

上面是當時的實測結論。好消息是現代瀏覽器已經統一支援標準屬性:overflow-wrap: break-word 能在單詞間優先斷行、必要時才硬切,word-break: break-word 則可作為舊版相容退路。今天要處理長網址或無空白字串,寫 overflow-wrap: anywhere 加上容器 max-width: 100% 就能跨瀏覽器解決,不必再靠 hack。

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