自用笔记:本文属于自用笔记,不做详解,仅供参考。
在 CSS 中应用 ellipsis 属性,可显示省略符号(…)来代表被修剪的文本。
See the Pen text-overflow: ellipsis by MOxFIVE (@MOxFIVE) on CodePen.
单行省略
样式代码
1 2
| <span>CSS is awesome, especially when you can scroll to see extra text instead of just having it overlap other text by default.</span>
|
1 2 3 4 5 6 7
| span { display: block; width: 14em; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
|
代码简析
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| text-overflow: ellipsis;
display: block;
white-space: nowrap;
overflow: hidden;
width: 14em;
|
多行省略
在指定行数后,才使用省略号代替溢出文本,这可以借助 -webkit-line-clamp
属性实现
- -webkit- 内核属性,不支持 IE 和 Firefox 浏览器 查看兼容性
样式代码
1 2
| <p>CSS is awesome, especially when you can scroll to see extra text instead of just having it overlap other text by default.</p>
|
1 2 3 4 5 6 7 8 9
| p { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; width: 15em; line-height: 1.5; text-overflow: ellipsis; overflow : hidden; }
|
代码简析
1 2 3 4 5 6 7 8 9 10
| display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; text-overflow: ellipsis; overflow : hidden;
width: 15em; line-height: 1.5;
|
相关链接
- 多行文本溢出显示省略号(…)全攻略 by 愚人码头 on
2014/09/30
: http://www.css88.com/archives/5206
- Overflow Ellipsis: the text-overflow property by W3C on
2015/07/07
: https://www.w3.org/TR/css-ui-3/#text-overflow