一、单行文本溢出处理
1. 基础方案 - 使用 text-overflow
.single-line {
width: 200px; /* 必须设置宽度 */
white-space: nowrap; /* 强制不换行 */
overflow: hidden; /* 溢出隐藏 */
text-overflow: ellipsis; /* 溢出显示省略号 */
}
2. 完整示例
/* 基础单行省略 */
.text-ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* 带最大宽度的响应式方案 */
.responsive-ellipsis {
max-width: 100%;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* 表格单元格优化 */
.table-cell {
max-width: 150px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
3. 自定义省略符
/* 自定义省略符号 */
.custom-ellipsis {
width: 200px;
white-space: nowrap;
overflow: hidden;
text-overflow: "…"; /* 支持自定义,但兼容性有限 */
}
/* 多语言适配 */
.ellipsis-ar {
direction: rtl; /* 阿拉伯语等从右到左语言 */
text-align: right;
}
二、多行文本溢出处理
1. 标准方案 - 使用 -webkit-line-clamp(推荐)
.multi-line {
display: -webkit-box; /* 旧版弹性盒模型 */
-webkit-box-orient: vertical; /* 垂直排列 */
-webkit-line-clamp: 3; /* 限制行数 */
overflow: hidden; /* 溢出隐藏 */
text-overflow: ellipsis; /* 省略号 */
line-height: 1.5; /* 推荐设置行高 */
max-height: calc(1.5em * 3); /* 计算最大高度,可选 */
}
2. 兼容性更好的写法
.multi-line-safe {
overflow: hidden;
text-overflow: ellipsis;
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
/* 非webkit内核备用方案 */
line-height: 1.5;
max-height: 4.5em; /* line-height * 行数 */
}
3. 行数控制变量化
:root {
--line-clamp: 3;
--line-height: 1.5;
}
.multi-line-var {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: var(--line-clamp);
overflow: hidden;
text-overflow: ellipsis;
line-height: var(--line-height);
max-height: calc(var(--line-height) * 1em * var(--line-clamp));
}
三、JavaScript 解决方案
1. 纯JS实现多行省略
/**
* 多行文本省略
* @param {HTMLElement} element - 目标元素
* @param {number} lineCount - 限制行数
*/
function textEllipsis(element, lineCount) {
const lineHeight = parseInt(getComputedStyle(element).lineHeight);
const maxHeight = lineHeight * lineCount;
if (element.scrollHeight > maxHeight) {
// 截断文本并添加省略号
let text = element.textContent;
while (element.scrollHeight > maxHeight && text.length > 0) {
text = text.slice(0, -1);
element.textContent = text + '...';
}
}
}
// 使用
const element = document.querySelector('.text');
textEllipsis(element, 3);
2. 更智能的截断算法
function smartEllipsis(element, options = {}) {
const {
lineCount = 3,
ellipsis = '...',
wordBreak = false
} = options;
const lineHeight = parseInt(getComputedStyle(element).lineHeight);
const maxHeight = lineHeight * lineCount;
if (element.scrollHeight <= maxHeight) return;
const originalText = element.textContent;
let left = 0;
let right = originalText.length;
let result = '';
// 二分查找截断点
while (left <= right) {
const mid = Math.floor((left + right) / 2);
const text = originalText.slice(0, mid) + ellipsis;
element.textContent = text;
if (element.scrollHeight <= maxHeight) {
result = text;
left = mid + 1;
} else {
right = mid - 1;
}
}
element.textContent = result || ellipsis;
}
四、React/Vue 组件方案
1. React 组件示例
import React, { useState, useRef, useEffect } from 'react';
const TextEllipsis = ({
children,
line = 1,
ellipsis = '...',
className = ''
}) => {
const containerRef = useRef(null);
const [isOverflow, setIsOverflow] = useState(false);
useEffect(() => {
if (!containerRef.current) return;
const element = containerRef.current;
const lineHeight = parseInt(getComputedStyle(element).lineHeight);
const maxHeight = lineHeight * line;
setIsOverflow(element.scrollHeight > maxHeight);
}, [children, line]);
const style = line === 1
? {
overflow: 'hidden',
textOverflow: 'ellipsis',
whiteSpace: 'nowrap'
}
: {
display: '-webkit-box',
WebkitBoxOrient: 'vertical',
WebkitLineClamp: line,
overflow: 'hidden',
textOverflow: 'ellipsis'
};
return (
<div
ref={containerRef}
style={style}
className={className}
title={isOverflow ? children : ''}
>
{children}
</div>
);
};
// 使用
<TextEllipsis line={3}>
这是一段很长的文本内容...
</TextEllipsis>
2. Vue 指令方案
<template>
<div v-ellipsis:2 class="content">
这是一段很长的文本内容...
</div>
</template>
<script>
export default {
directives: {
ellipsis: {
inserted(el, binding) {
const lineCount = binding.arg || 1;
if (lineCount === 1) {
el.style.overflow = 'hidden';
el.style.textOverflow = 'ellipsis';
el.style.whiteSpace = 'nowrap';
} else {
el.style.display = '-webkit-box';
el.style.webkitBoxOrient = 'vertical';
el.style.webkitLineClamp = lineCount;
el.style.overflow = 'hidden';
el.style.textOverflow = 'ellipsis';
}
}
}
}
}
</script>
五、特殊场景处理
1. 响应式文本省略
/* 移动端多行,桌面端单行 */
.responsive-text {
overflow: hidden;
text-overflow: ellipsis;
/* 移动端:最多3行 */
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
/* 桌面端:单行 */
@media (min-width: 768px) {
white-space: nowrap;
display: block;
-webkit-line-clamp: unset;
}
}
2. 表格内的文本省略
.table-cell-ellipsis {
max-width: 150px;
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
/* 悬停显示完整内容 */
&:hover {
overflow: visible;
white-space: normal;
position: relative;
z-index: 10;
background: white;
box-shadow: 0 2px 8px rgba(0,0,0,0.15);
}
}
3. 带展开/收起功能
<div class="expandable-text">
<div class="text-content" :class="{ truncated: isTruncated }">
{{ longText }}
</div>
<button @click="isTruncated = !isTruncated">
{{ isTruncated ? '展开' : '收起' }}
</button>
</div>
<style>
.text-content {
display: -webkit-box;
-webkit-box-orient: vertical;
overflow: hidden;
}
.text-content.truncated {
-webkit-line-clamp: 3;
}
</style>
六、最佳实践与注意事项
兼容性处理
/* 多行省略的兼容性写法 */
.multi-line-fallback {
position: relative;
line-height: 1.5em;
max-height: 4.5em; /* 3行 */
overflow: hidden;
padding-right: 1em; /* 为省略号留空间 */
}
.multi-line-fallback::after {
content: '...';
position: absolute;
bottom: 0;
right: 0;
background: white; /* 背景色遮盖 */
padding-left: 0.5em;
}
性能优化建议
- 避免频繁重排:不要在滚动等高频事件中计算文本溢出
- 使用防抖/节流:如果需要在resize时重新计算
- 缓存计算结果:对于静态内容,计算一次即可
- 优先使用CSS方案:性能优于JavaScript方案
可访问性考虑
.ellipsis-accessible {
/* 基础省略样式 */
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
/* 确保屏幕阅读器能获取完整内容 */
&[title] {
cursor: help;
}
}
七、实用工具类
/* 单行省略 */
.ellipsis {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
/* 多行省略 - 2行 */
.line-clamp-2 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 2;
overflow: hidden;
}
/* 多行省略 - 3行 */
.line-clamp-3 {
display: -webkit-box;
-webkit-box-orient: vertical;
-webkit-line-clamp: 3;
overflow: hidden;
}
/* 响应式省略 */
@media (max-width: 768px) {
.ellipsis-mobile {
overflow: hidden;
text-overflow: ellipsis;
white-space: nowrap;
}
}
总结选择策略
| 需求场景 | 推荐方案 | 备注 |
|---|---|---|
| 单行文本省略 | text-overflow: ellipsis | 兼容性好,性能最佳 |
| 多行文本省略(WebKit内核) | -webkit-line-clamp | 现代浏览器首选 |
| 多行文本省略(全兼容) | JS计算 + CSS备用 | 考虑兼容性的选择 |
| 动态内容/复杂布局 | JavaScript方案 | 更灵活可控 |
| 需要展开/收起功能 | JS方案 + 状态控制 | 交互需求复杂时 |
| 移动端优先 | -webkit-line-clamp | 移动端WebKit内核普及 |
根据实际项目需求、浏览器兼容性要求和性能考虑选择合适的方案。