30天挑戰:「CSS 多行文字溢出後改為顯示刪節號」技術記錄

總結

text-overflow: ellipsis;僅能處理單行文字,若想要讓多行文字的尾端改為顯示刪節號,需使用-webkit-line-clamp

See the Pen text-overflow ellipsis by Charlie (@Charlie7779) on CodePen.

應用(卡片標題與內文):

效果展示

展示位置:https://tzynwang.github.io/ac_challenge_2-3F_profile-card/ 原始碼:https://github.com/tzynwang/ac_challenge_2-3F_profile-card

筆記

單行文字

MDN: The text-overflow property doesn’t force an overflow to occur. To make text overflow its container you have to set other CSS properties: overflow and white-space.

多行文字

MDN: It only works in combination with the display property set to -webkit-box or -webkit-inline-box, and the -webkit-box-orient property set to vertical.

MDN: In most cases you will also want to set overflow to hidden, otherwise the contents won’t be clipped but an ellipsis will still be shown after the specified number of lines.

參考文件