欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

CSS截取字符串,多余文字省略号显示

<style type="text/css">
body{
font-family:Arial, Helvetica, sans-serif;
font-size:12px;
}
div{
width:200px;
height:24px;
line-height:24px;
overflow:hidden;
border:#ccc solid 1px;
background-color:#F9F9F9;
margin:5px;
}
div a{
color:#000;
display:block;
padding-right:7px;
background:url(http://www.365css.cn/example/ellipsis_365css.cn/ellipsis.gif) no-repeat right bottom;
}
</style>

<div><a href="#">CSS截取字符串,超出用省略号代替</a></div>
<div><a href="#">CSS截取字符串,并将超出用省略号代替</a></div>
<div><a href="http://www.hi.baidu.com/fzlibei" target="_blank">CSS</a></div>
<div><a href="http://www.hi.baidu.com/fzlibei" target="_blank"><span></span></a></div>

我看不明白上面的代码,请帮忙写下注释~!
还有把省略号的图片换成字符串~!

谢谢~!
收起
wkd1989 | 浏览 19996 次  问题未开放回答 |举报
推荐于2017-09-23 18:45:18最佳答案
 

使用text-overflow属性可实现css截取字符串功能。

示例:

1
#test{text-overflow:ellipsis;white-space:nowrap;overflow:hidden;width:20px;}

上面的样式会将id为test的元素超出20像素之外的文字隐藏,并替换为三个英文半角的句号...

text-overflow主要支持2个值:ellipsis为显示省略号,clip为修剪文本,是默认值。

white-space:nowrap;的作用是不让元素中的内容折行,在一行显示。

overflow:hidden;为溢出隐藏,目的是超过元素宽高的部分不再显示。

width:20px;设置了元素的宽度,内容超出这个宽度后会显示省略号。

来自 https://zhidao.baidu.com/question/128695943.html


普通分类: