用 CSS 控制中英文混排的強制換行並兩端對齊,能有效避免內容溢出容器而撐壞版面。這個技巧看起來樸素,在處理使用者生成內容或後台稿件的頁面時非常實用。
<html> <head> <title>CSS強制換行對齊</title> </head> <body> 英文對齊: <div style="font-size:12px; width:300px; text-align:justify; text-justify:newspaper"> If you are Male, between 20 to 40 years old, ethnically Chinese and effectively bilingual (English/Mandarin) with a minimum university degree, this is your opportunity of a lifetime. You need to have confidence and charisma in front of the camera and possess good scriptwriting ability in English. Some Media experience preferred </div> 中文對齊: <div style="font-size:12px; width:300px; text-align:justify; text-justify:inter-ideograph"> 一款灰色風格超實用漂亮網頁日曆組件,在保證外觀的基礎上,本插件更注重功能的體驗, 操作很舒服,想使用網頁日期選擇器的朋友,本款是您不錯的選擇。 </div> </body> </html>
text-align: justify:兩端對齊。它會調整字元間距,讓每一行(除最後一行)的寬度都填滿容器。段落排版看起來整齊,是長文閱讀的基本設定。
text-justify:決定「怎麼分配」多出來的空間,取值隨語言而異。
newspaper:英報社排版演算法,主要透過加寬字距來對齊,適合英文段落。
inter-ideograph:針對表意文字(中日韓)最佳化,會均勻調整字距,中文段落用這個值最自然。
原範例的 width:300 少了單位,正式環境請務必寫成 width: 300px,否則宣告會被瀏覽器直接忽略。
另外,遇到長網址或連續英文單字撐破容器時,justify 是無效的,需要搭配斷行規則:
.wrap {
word-wrap: break-word; /* 允許在任意位置斷行 */
word-break: break-all; /* 更激進,亞洲文字排版常用 */
overflow-wrap: anywhere; /* 現代寫法,兼顧 min-content 計算 */
}
中文段落還建議加上 text-indent: 2em 做首行縮排,並用 line-height: 1.7 左右的行距提升可讀性——這三個屬性加起來,就是中文網頁正文的基本體面。