文本超过长度显示'...'的一个办法

本来我是用{{(item.title.length<38)?item.title:(item.title.substr(0,38)+'...')}}来判断标题的长度,超过长度,截取前38个字符,但是中英文字符的宽度不一样,导致不整齐。
于是使用如下的css方法。text-overflow:ellipsis;

<!DOCTYPE html>
<html>
<head>
<style> 
a.test {
display:block;//必要
white-space:nowrap; 
width:12em; //必要
overflow:hidden; //必要
text-overflow:ellipsis;//核心
}
div.test
{
white-space:nowrap; 
width:12em; 
overflow:hidden; 
border:1px solid #000000;
text-overflow:clip;
}
</style>
</head>
<body>

<p>下面两个 div 包含无法在框中容纳的长文本。正如您所见,文本被修剪了。</p>

<p>这个 a 使用 "text-overflow:ellipsis" :</p>

<a class="test">This is some long text that will not fit in the box</a>

<p>这个 div 使用 "text-overflow:clip":</p>

<div class="test">This is some long text that will not fit in the box</div>

</body>
</html>
最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容