The Most Comprehensive Collection of 'Text Overflow Truncation' Solutions
This article explores various methods for handling text overflow truncation in web development, including CSS solutions like -webkit-line-clamp and JavaScript implementations, discussing their advantages, limitations, and practical applications.
👆 想要了解更多不掺水的原创,请戳上方蓝色字体: 政采云前端团队 关注我们吧~
本文首发于政采云前端团队博客:可能是最全的 “文本溢出截断省略” 方案合集 https://www.zoo.team/article/text-overflow
前言
在我们的日常开发工作中,文本溢出截断省略是很常见的一种需考虑的业务场景细节。 看上去 “稀松平常” ,但在实现上却有不同的区分,是单行截断还是多行截断? 多行的截断判断是基于行数还是基于高度? 这些问题之下,都有哪些实现方案? 他们之间的差异性和场景适应性又是如何? 凡事就怕较真,较真必有成长。 本文试图通过编码实践,给出一些答案。
先来点基础的,单行文本溢出省略
核心 CSS 语句
overflow: hidden;(文字长度超出限定宽度,则隐藏超出的内容)
white-space: nowrap;(设置文字在一行显示,不能换行)
text-overflow: ellipsis;(规定当文本溢出时,显示省略符号来代表被修剪的文本)
优点
无兼容问题
响应式截断
文本溢出范围才显示省略号,否则不显示省略号
省略号位置显示刚好
短板
只支持单行文本截断
适用场景
适用于单行文本溢出显示省略号的情况
Demo
<code style="line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; padding: 0.5em; color: rgb(186, 186, 186); display: -webkit-box !important">.demo {<br/> white-space: nowrap;<br/> overflow: hidden;<br/> text-overflow: ellipsis;<br/>}<br/><<span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">/style><br/><body><br/> <div class="demo">这是一段很长的文本</</span>div><br/><span style="font-size: inherit; color: inherit; line-height: inherit"></<span style="font-size: inherit; line-height: inherit; color: rgb(203, 120, 50)">body</span>></span><br/></code>示例图片
进阶一下,多行文本溢出省略(按行数)
○ 纯 CSS 实现方案
核心 CSS 语句
-webkit-line-clamp: 2;(用来限制在一个块元素显示的文本的行数,2 表示最多显示 2 行。为了实现该效果,它需要组合其他的 WebKit 属性)
display: -webkit-box;(和 1 结合使用,将对象作为弹性伸缩盒子模型显示 )
-webkit-box-orient: vertical;(和 1 结合使用 ,设置或检索伸缩盒对象的子元素的排列方式 )
overflow: hidden;(文本溢出限定的宽度就隐藏内容)
text-overflow: ellipsis;(多行文本的情况下,用省略号 “…” 隐藏溢出范围的文本)
优点
响应式截断
文本溢出范围才显示省略号,否则不显示省略号
省略号显示位置刚好
短板
兼容性一般:-webkit-line-clamp 属性只有 WebKit 内核的浏览器才支持
适用场景
多适用于移动端页面,因为移动设备浏览器更多是基于 WebKit 内核
Demo
<code style="line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; padding: 0.5em; color: rgb(186, 186, 186); display: -webkit-box !important">.demo {<br/> display: -webkit-box;<br/> overflow: hidden;<br/> -webkit-line-clamp: 2;<br/> -webkit-box-orient: vertical;<br/>}<br/><<span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">/style><br/><br/><body><br/> <div class='demo'>这是一段很长的文本</</span>div><br/><span style="font-size: inherit; color: inherit; line-height: inherit"></<span style="font-size: inherit; line-height: inherit; color: rgb(203, 120, 50)">body</span>></span><br/></code>示例图片
○ 基于 JavaScript 的实现方案
优点
无兼容问题
响应式截断
文本溢出范围才显示省略号,否则不显示省略号
短板
需要 JS 实现,背离展示和行为相分离原则
适用场景
适用于响应式截断,多行文本溢出省略的情况
Demo
const text = '这是一段很长的文本' ; const totalTextLen = text.length; const formatStr = () => { const ele = document .getElementsByClassName( 'demo' )[ 0 ]; const lineNum = 2 ; const baseWidth = window .getComputedStyle(ele).width; const baseFontSize = window .getComputedStyle(ele).fontSize; const lineWidth = +baseWidth.slice( 0 , -2 ); // 所计算的strNum为元素内部一行可容纳的字数(不区分中英文) const strNum = Math .floor(lineWidth / +baseFontSize.slice( 0 , -2 )); let content = '' ; // 多行可容纳总字数 const totalStrNum = Math .floor(strNum * lineNum); const lastIndex = totalStrNum - totalTextLen; if (totalTextLen > totalStrNum) { content = text.slice( 0 , lastIndex - 3 ).concat( '...' ); } else { content = text; } ele.innerHTML = content; }; formatStr(); window .onresize = () => { formatStr(); }; < /body> 示例图片
○ 利用 Float 特性,纯 CSS 实现多行省略
核心 CSS 语句
line-height: 20px;(结合元素高度,高度固定的情况下,设定行高, 控制显示行数)
overflow: hidden;(文本溢出限定的宽度就隐藏内容)
float: right/left;(利用元素浮动的特性实现)
position: relative;(根据自身位置移动省略号位置, 实现文本溢出显示省略号效果)
word-break: break-all;(使一个单词能够在换行时进行拆分)
优点
无兼容问题
响应式截断
文本溢出范围才显示省略号,否则不显示省略号
短板
省略号显示可能不会刚刚好,有时会遮住一半文字
适用场景
适用于对省略效果要求较低,文本一定会溢出元素的情况
Demo
<code style="line-height: 18px; font-size: 14px; letter-spacing: 0px; font-family: Consolas, Inconsolata, Courier, monospace; padding: 0.5em; color: rgb(186, 186, 186); display: -webkit-box !important">.demo {<br/> background: #<span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">099</span>;<br/> max-height: <span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">40</span>px;<br/> line-height: <span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">20</span>px;<br/> overflow: hidden;<br/>}<br/> .demo::before{<br/> <span style="font-size: inherit; color: inherit; line-height: inherit">float</span>: <span style="font-size: inherit; line-height: inherit; color: rgb(203, 120, 50)">left</span>;<br/> content:<span style="font-size: inherit; line-height: inherit; color: rgb(106, 135, 89)">''</span>;<br/> width: <span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">20</span>px;<br/> height: <span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">40</span>px;<br/> }<br/><br/> .demo .text {<br/> <span style="font-size: inherit; color: inherit; line-height: inherit">float</span>: <span style="font-size: inherit; line-height: inherit; color: rgb(203, 120, 50)">right</span>;<br/> width: <span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">100</span>%;<br/> margin-left: <span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">-20</span>px;<br/> word-<span style="font-size: inherit; line-height: inherit; color: rgb(203, 120, 50)">break</span>: <span style="font-size: inherit; line-height: inherit; color: rgb(203, 120, 50)">break</span>-all;<br/> }<br/> .demo::after{<br/> <span style="font-size: inherit; color: inherit; line-height: inherit">float</span>:<span style="font-size: inherit; line-height: inherit; color: rgb(203, 120, 50)">right</span>;<br/> content:<span style="font-size: inherit; line-height: inherit; color: rgb(106, 135, 89)">"..."</span>;<br/> width: <span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">20</span>px;<br/> height: <span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">20</span>px;<br/> position: <span style="font-size: inherit; line-height: inherit; color: rgb(203, 120, 50)">relative</span>;<br/> left:<span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">100</span>%;<br/> transform: <span style="font-size: inherit; line-height: inherit; color: rgb(203, 120, 50)">translate</span>(<span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">-100</span>%,<span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">-100</span>%);<br/> }<br/><<span style="font-size: inherit; line-height: inherit; color: rgb(104, 150, 186)">/style><br/><br/><body><br/> <div class='demo'>这是一段很长的文本</</span>div><br/><span style="font-size: inherit; color: inherit; line-height: inherit"></<span style="font-size: inherit; line-height: inherit; color: rgb(203, 120, 50)">body</span>></span><br/></code>示例图片
原理讲解
有 A、B、C 三个盒子,A 左浮动,B、C 右浮动。设置 A 盒子的高度与 B 盒子高度(或最大高度)要保持一致
当 B 盒子高度低于 A 盒子,C 盒子仍会处于 B 盒子右下方。
如果 B 盒子文本过多,高度超过了 A 盒子,则 C 盒子不会停留在右下方,而是掉到了 A 盒子下。
接下来对 C 盒子进行相对定位,将 C 盒子位置向右侧移动 100%,并向左上方向拉回一个 C 盒子的宽高(不然会看不到哟)。这样在文本未溢出时不会看到 C 盒子,在文本溢出时,显示 C 盒子。
本文介绍了几种目前常见的文本截断省略的方案,各有利弊,各位同学可根据实际开发情况及需求选择方案。如果你还知道更好其他实现方案,欢迎在评论区留下宝贵评论。
参考文章
纯 CSS 实现多行文字截断 ( https://github.com/happylindz/blog/issues/12 )
【 CSS / JS 】限制一行和多行文字数量,溢出部分用省略号显示 ( https://blog.csdn.net/qq_40072782/article/details/82908581 )
HTML技巧篇:如何让单行文本以及多行文本溢出显示省略号(…) ( https://baijiahao.baidu.com/s?id=1621362934713048315&wfr=spider&for=pc )
看 完两件事
如果你觉得这篇内容对你挺有启发,我想邀请你帮我两件小事
1.点个「 在看 」,让更多人也能看到这篇内容(喜欢不点在看的,都是耍流氓)
2.关注公众号「 政采云前端团队
招贤纳士
政采云前端团队(ZooTeam),一个年轻富有激情和创造力的前端团队,隶属于政采云产品研发部,Base 在风景如画的杭州。团队现有 50 余个前端小伙伴,平均年龄 27 岁,近 3 成是全栈工程师,妥妥的青年风暴团。成员构成既有来自于阿里、网易的“老”兵,也有浙大、中科大、杭电等校的应届新人。团队在日常的业务对接之外,还在物料体系、工程平台、搭建平台、性能体验、云端应用、数据分析及可视化等方向进行技术探索和实战,推动并落地了一系列的内部技术产品,持续探索前端技术体系的新边界。
如果你想改变一直被事折腾,希望开始能折腾事;如果你想改变一直被告诫需要多些想法,却无从破局;如果你想改变你有能力去做成那个结果,却不需要你;如果你想改变你想做成的事需要一个团队去支撑,但没你带人的位置;如果你想改变既定的节奏,将会是“ 5 年工作时间 3 年工作经验”;如果你想改变本来悟性不错,但总是有那一层窗户纸的模糊… 如果你相信相信的力量,相信平凡人能成就非凡事,相信能遇到更好的自己。如果你希望参与到随着业务腾飞的过程,亲手推动一个有着深入的业务理解、完善的技术体系、技术创造价值、影响力外溢的前端团队的成长历程,我觉得我们该聊聊。任何时间,等着你写点什么,发给
[email protected]Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
政采云技术
ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
