DIV CSS 佈局教程網

您现在的位置: DIV+CSS佈局教程網 >> 網頁腳本 >  >> CSS入門知識 >> DIV十CSS布局 >> 布局實例
長串英文不換行把版面撐破?一個屬性解決
編輯:布局實例     

中文在儲存格裡超出寬度會自動換行,但純英文不會。不論你把 TD 寬度設成多少,連續的英文字元就是一路向右延伸,把版面撐爛。做英文站或動態輸出使用者內容時,這是個很麻煩的問題。

幸好解法很簡單,一個屬性即可:word-break: break-all。

示範程式碼

<table style="table-layout:fixed">
<tr>
<td width="100" style="word-break:break-all">Mistletoe also known throughout history as the golden bough has several different legends associated with its beginning and its meaning.

Starting back as far as the Celtic Druids you can find more than one story concerning the mystical mistletoe. One such story is their belief that the plant held the soul of the host tree, which was the "holy oak". During a ceremony, the Druid priests would harvest the mistletoe with a golden sickle to ensure the mistletoe never touched the ground, as it would lose its magical powers.

In England, mistletoe at one time was placed over the doorways for good luck. They believed that only happiness could pass underneath the mistletoe, therefore, enemies would hug and seal their peaceful intentions with a kiss of friendship.

The ancient Norse people have a wonderful legend centered on the mistletoe. In their legend, the god Odin and his wife Frigga had a son by the name of Balder. Frigga loved her son very dearly. She took steps to ensure that nothing would ever harm him by way of earth, fire, water, or air.

The mistletoe did not fit into any of these categories, so an evil spirit by the name of Loki created an arrow out of mistletoe and gave it to Balder's brother, Holder. Holder was blind and Loki held onto his hand and shot Balder in the heart with the mistletoe arrow. Balder died.

From that day, forward, Frigga stated the mistletoe would never harm anyone again and made it a symbol of love and made the promise to bestow a kiss upon everyone who passed beneath it.
</td>
</tr>
</table>

三個屬性怎麼挑

這裡其實牽涉到兩件事,容易混淆:

  • table-layout: fixed:讓表格採用固定佈局演算法,寬度由第一列決定,內容再長也不會把欄位撐開。這是「容器不被撐破」的關鍵。
  • word-break: break-all:允許在任意字元處斷行,不必等到單詞邊界。這是「文字真的換行」的關鍵。

若你希望英文單詞保持完整、只在必要時才切斷,可以改用 word-wrap: break-word(或現代的 overflow-wrap: break-word),它會優先在單詞間斷行,實在放不下才硬切。若內容包含超長網址或無空白字串,兩者搭配使用最穩妥。

今天的標準寫法可以再加上 word-break: break-word 作為相容退路,並在容器上補 max-width: 100%,確保圖片與長字串都不會撐破父層。

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