單行文字溢出時,可讓超出部分顯示省略號或截斷,前提是必須設定寬度。核心樣式為:
width: 300px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
若只想截斷而不顯示省略號,可將 text-overflow 設為 clip。

但這個屬性只支援單行文字溢出顯示省略號。若要在多行文字上實現類似效果,就需要另一套做法。
利用 WebKit 的 CSS 擴展屬性即可實現多行省略:
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;

適用範圍:因使用了 WebKit 的 CSS 擴展屬性,該方法適用於 WebKit 瀏覽器及行動端。
其中幾個關鍵屬性的作用如下:
另一種做法是透過偽元素搭配漸變背景來模擬省略號:
p { position: relative; line-height: 20px; max-height: 40px; overflow: hidden; }
p::after {
content: "...";
position: absolute;
bottom: 0; right: 0;
padding-left: 40px;
background: -webkit-linear-gradient(left, transparent, #fff 55%);
background: -o-linear-gradient(right, transparent, #fff 55%);
background: -moz-linear-gradient(right, transparent, #fff 55%);
background: linear-gradient(to right, transparent, #fff 55%);
}
適用範圍:此法適用範圍廣,但即使文字未超出也會顯示省略號,可搭配 JS 優化。
使用時需注意幾點:
<span>…</span>)兼容;兼容 IE8 則需將 ::after 替換成 :after。