要實現「單行文字超寬度顯示省略號」,必須同時設定三個 CSS 屬性:
.ellipsis {
white-space: nowrap; /* 強制不換行 */
overflow: hidden; /* 隱藏溢出內容 */
text-overflow: ellipsis; /* 溢出處顯示省略號 */
width: 200px; /* 必須有寬度限制 */
}
| 設定組合 | 結果 | 說明 |
|---|---|---|
僅 text-overflow:ellipsis | ❌ 無效 | 缺少溢出隱藏與不換行 |
text-overflow:ellipsis; white-space:nowrap | ❌ 無效 | 缺少 overflow:hidden |
| 三者同時設定 | ✅ 成功 | 完整滿足條件 |
<div style="width:100px;height:20px;text-overflow:ellipsis; white-space:nowrap; overflow:hidden;">
a b c d e f g h i j k l , msa sd sd sa w df f
</div>
渲染結果:a b c d e f g h i j k l , msa sd sd sa w df f 會在 100px 處截斷並顯示 ...。
-webkit-line-clamp(WebKit 瀏覽器)或 JS 方案。text-overflow 僅為「顯示提示」,本身不具備截斷能力,需配合 overflow 與 white-space。.multi-line-ellipsis {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3; /* 顯示 3 行 */
overflow: hidden;
}
非 WebKit 瀏覽器需採用 JavaScript 計算高度截斷。